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.