Send variables to PHP via Ajax - undefined variables

1

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

A brief description of the structure used (maybe this will influence the error): I have a main file index.php that uses a class for paging Result_page.php . In this class, if it is the last record, the (HTML) button does not display the value "Forward", but rather "Finish", and should record variables that identify the series accessed in the DB.

In the class Pagina_result.php:

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 related 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>

Finally, the file sessUser.php , which 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 have tried everything, but I can not find the error ... Although the variables are undefined, the recording is done in the BD, but with the values = 0.

    
asked by anonymous 06.01.2016 / 15:23

3 answers

1

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

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

Try this code below and see 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);

?>
    
06.01.2016 / 20:29
1

Function does not find variables, consider using global in 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 a 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);
    ?>
    
06.01.2016 / 20:46
1

In the sessUser.php file, you are redeeming $_POST and assigning variables outside the scope of the inclusao() function.

Move the 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 global

function inclusao() {
    global $servico, $sessao, $user
    
06.01.2016 / 21:13