Send variables to PHP via Ajax - undefined variables

Asked

Viewed 410 times

1

I’m trying to send data from one PHP file to another via Ajax, but the variables are going undefined (in Alert, it displays: "Notice: Undefined variable:").

A brief description of the structure used (maybe this influences the error): I have a main file index php. using a class for result paging Pagina_result.php. In this class, if it is the last record, the button (in HTML) does not display the "Next" value, but "Finish", and should write variables that identify the accessed series in the BD.

In the Pagina_result.php class:

if ( $current_page != $total_of_pages ) { 
            /*Monta o "Avançar"*/
        } else {
            print " <button type=\"button\" value=\"Finalizar\" class=\"Accesso\" id=\"finalizar\"> ";

        }

The definition of variables in PHP:

<?php
    $servico    = ( isset( $_GET['servico'] ) ? $_GET['servico'] : 0 );
    $sessao     = ( isset( $_GET['id'] )   ? $_GET['id']   : 0 );
    $user       = ( isset( $_SESSION['usrid'] )   ? $_SESSION['usrid']   : 0 );
?>

The Ajax code to send the variables relating to the item for recording:

<script language="javascript">
    $(document).ready(function(){
        $("#finalizar").click( function() {
           $.ajax({
              type:'post',
              url:'sessUser.php',
              data:{ 'servico': <?php echo $servico; ?>, 'sessao': <?php echo $sessao; ?>, 'user': <?php echo $user; ?>, }
            }).done( function( data ) {
                alert(data);
            });
        });
    });

    </script>

And finally, the file sessUser.php, that should receive the data:

<?php

$servico = $_POST['servico'];
$sessao = $_POST['sessao'];
$user = $_POST['user'];

function inclusao() {
    $link = mysql_connect("localhost", "root", "");
    mysql_select_db("db", $link);
    $inserir = mysql_query("INSERT INTO tabela (id_servico, id_sessao, id_user)values('".$servico."','".$sessao."','".$user."')", $link);
}

inclusao();
?> 

I’ve tried everything, but I can’t find the error... Although the variables are undefined, the recording is done in BD, but with values = 0.

  • Are all these post variables strings?

  • How does this jQuery appear on the client side? (already rendered)

  • @Diego Felipe the first two variables are strings. The "user" variable comes from a Session with the user ID (incremental int in the BD).

  • @Sergio you want to know about the rendering in the page source code?

  • Yes, I want to see how this date appears inside the ajax object to try to figure out what the error might be...

  • He gets like this: <script language="javascript">&#xA; $(document).ready(function(){&#xA; $("#finalizar").click( function() {&#xA; $.ajax({&#xA; type:'post',&#xA; url:'sessUser.php',&#xA; data:{ 'servico': 20, 'sessao': 15, 'user': 8, }&#xA; }).done( function( data ) {&#xA; alert(data);&#xA; });&#xA; });&#xA; });&#xA; &#xA; </script> Apparently fills the date with the data I wanted, but does not send forward...

Show 1 more comment

3 answers

1


I believe the problem is in the file sessUser.php.

Variables are being set outside the function and used within the function, which can cause zeroed values.

Try this code below and check if it solves the problem:

<?php

$servico = $_POST['servico'];
$sessao = $_POST['sessao'];
$user = $_POST['user'];

$link = mysql_connect("localhost", "root", "");
mysql_select_db("db", $link);
$inserir = mysql_query("INSERT INTO tabela (id_servico, id_sessao, id_user)values('".$servico."','".$sessao."','".$user."')", $link);

?>

1

The function does not find the variables, consider using global in the function

<?php

$servico = $_POST['servico'];
$sessao = $_POST['sessao'];
$user = $_POST['user'];

function inclusao() {
    global $servico, $sessao, $user;
    $link = mysql_connect("localhost", "root", "");
    mysql_select_db("db", $link);
    $inserir = mysql_query("INSERT INTO tabela (id_servico, id_sessao, id_user)values('".$servico."','".$sessao."','".$user."')", $link);
}

inclusao();
?> 

or pass as parameter to the function

<?php

    $servico = $_POST['servico'];
    $sessao = $_POST['sessao'];
    $user = $_POST['user'];

    function inclusao($servico, $sessao, $user) {
        $link = mysql_connect("localhost", "root", "");
        mysql_select_db("db", $link);
        $inserir = mysql_query("INSERT INTO tabela (id_servico, id_sessao, id_user)values('".$servico."','".$sessao."','".$user."')", $link);
    }

    inclusao($servico, $sessao, $user);
    ?>

1

In the archive sessUser.php, is rescuing the $_POST and assigning variables outside the scope of the function inclusao().

Move variables into the function:

<?php


function inclusao() {

    $servico = $_POST['servico'];
    $sessao = $_POST['sessao'];
    $user = $_POST['user'];

    $link = mysql_connect("localhost", "root", "");
    mysql_select_db("db", $link);
    $inserir = mysql_query("INSERT INTO tabela (id_servico, id_sessao, id_user)values('".$servico."','".$sessao."','".$user."')", $link);
}

inclusao();
?> 

Another way is to pass parameters

inclusao($servico, $sessao, $user)

or invoke the global

function inclusao() {
    global $servico, $sessao, $user

Browser other questions tagged

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