Variable in Ajax request

0

Code to receive numeroCartao of user via $_Session and show all information about it. And from here I can delete this user via AJAX.

In this section of the code I can see the array containing all user data, however in the processaDadosExcluir.php file I do not find the same array, which causes a undefined variable error. And this prevents you from running SQL.

How can I pass a variable to the file processaDadosExcluir.php so that it is recognized in this file in order to execute the query?

<?php ?>
<script type="text/javascript">    
    $('#excluirdep').submit(function(event) {
        event.preventDefault();
        var valores = $('#excluirdep').serialize();

        console.log(valores);

         $.ajax({
            type: 'POST',
            url: 'ajax/processaDadosExcluir.php',
            data: valores,
            dataType: 'html',
            success: function (data) {
                    console.log(data);
                    $("#excluirDependente").html(data);
            }
        });
    });
</script>    
<?php

echo "Excluir pernamente o usuario: <br> Nome: ".$linha['NomeBeneficiario']."<br>
      Numero da Carteira: ".$_SESSION ['NumeroCartao']."<br>";   

 /*variaveis de sessao*/
$numeroCartao  = $_SESSION['NumeroCartao'];

//conexao
$con    =   mysql_connect("10.6.0.27","root","prtdb") or die("Erro na conexao!");
$db     =   mysql_select_db("teste001", $con) or die("Erro na selecao do banco");

//query
$sql = "DELETE FROM PlanAssiste WHERE NumeroCartao =".$numeroCartao;

//executar a query
$query = mysql_query($sql,$con) or die("Erro na Query-3: ".mysql_error());

//resposta
if ($query == NULL){    
    $return['msg'] = " <a href='#' class='close'>Fechar [X]</a> <br> <b> Nullo! </B>";
    echo $return['msg'];
}
else{
    $return['msg'] = "<div id='excluir' class='alter'>  
                         <p><label> Excluido com Sucesso! </label> </p>
                         <p><a href='javascript:history.back(0)' >Fechar [X]</a> </p> 
                     </div>

                    <style>
                        .alter{
                            width: 240px;
                            height: 200px;
                            color: red;
                        }
                    </style> ";
    echo $return['msg'];
}
?>

    
asked by anonymous 01.07.2014 / 03:08

1 answer

1

Fixed

In the page where I make the ajax request, I created a hidden type input with the name of the number in the form, which I use to collect the data to be sent via ajax to processedDataExclude.php

 <FORM id="excluirdep" name="excluirdep">
        <input name = "numeroCartao" id='numeroCartao' type="hidden" value='<?php echo $numeroCartao; ?>'  />

Well, in the javascript code snippet I changed a line:

<script type="text/javascript">

$('#excluirdep').submit(function(event) {
    event.preventDefault();
    var valores = $('#excluirdep').serialize();
    //variavel criada para receber o valor do imput hidden com o dado que quero
    var numerocartao = $("input[name=numeroCartao]").val();
    console.log(valores);

     $.ajax({
        type: 'POST',
        url: 'ajax/processaDadosExcluir.php',
        data: {primeiro:valores,segundo:numerocartao},
        dataType: 'html',
        success: function (data) {
                console.log(data);
                $("#excluirDependente").html(data);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(thrownError);
        }
    });
});

In the php part, I just put a line:

$numeroCartao = $_POST["segundo"];

Here you retrieved the id value to perform the sql search and delete the user data!

    
02.07.2014 / 18:10