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?
– user28595
How does this jQuery appear on the client side? (already rendered)
– Sergio
@Diego Felipe the first two variables are strings. The "user" variable comes from a Session with the user ID (incremental int in the BD).
– Atoyansk
@Sergio you want to know about the rendering in the page source code?
– Atoyansk
Yes, I want to see how this date appears inside the ajax object to try to figure out what the error might be...
– Sergio
He gets like this:
<script language="javascript">
 $(document).ready(function(){
 $("#finalizar").click( function() {
 $.ajax({
 type:'post',
 url:'sessUser.php',
 data:{ 'servico': 20, 'sessao': 15, 'user': 8, }
 }).done( function( data ) {
 alert(data);
 });
 });
 });
 
 </script>
Apparently fills the date with the data I wanted, but does not send forward...– Atoyansk