Variable in Ajax request

Asked

Viewed 518 times

0

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

In this excerpt of the code I can see the array containing all the user data, but in the file processaDadosExcluir.php cannot find the same array, which generates an error undefined variable. And this prevents 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'];
}
?>

inserir a descrição da imagem aqui

  • What’s the name of the file you put on top?

  • 1

    After having edited, I’m under the impression that you put the code of your two files without any identification or separation when doing the copy/Paste (arquivo1.php -> código + arquivo2.php -> código).

  • Exactly, there are two files...user.php and processDadosExcluir

1 answer

1


Solved

On the page where I do the ajax request, I created an Hidden input with number nameCartao inside the form which I use to collect the data to be sent via ajax to processDadosExcluir.php

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

Poes well, in the javascript code 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 recovered id value to perform sql search and erase user data!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.