'can't use my retrieved variable after ajax Js PHP
basically im trying to retrive a Js variable and send to php using ajax in order to add it to the data base ....and it works and it's added to the database but i can't use that variable in the php code (i need it in order to get the lastInsrtId() and i also need to echo it)
here is my JS code
let x;
function tableText(tableCell) {
x =tableCell.innerHTML;
console.log(x);
$.ajax({
type: 'POST',
url: 'reservation.php',
dataType: "json",
data: { x:x },
complete: function(text){
console.log(text);
return text; }
});
}
function tableText(tableCell) {
x =tableCell.innerHTML;
console.log(x);
$.ajax({
type: 'POST',
url: 'try.php',
dataType: "json",
data: { x:x },
success : function(text){
$('#MyDiv').html(text);
}
});
}
and then my PHP code
json_encode(['text'=>'ok']);
$Salle=$_POST['x'];
echo $Salle;
$bdd->beginTransaction();
$requete = $bdd->prepare('INSERT INTO reservation (code_salle,date_res,code_cr,code_profe)
VALUE (:code_salle,:date_res,:code_cr,:code_profe)');
$requete->execute(array(
'code_salle'=>$Salle,
'date_res'=>$date,
'code_cr'=>$heure,
'code_profe'=>$prof,
));
$last_id = $bdd->lastInsertId();
echo "New record created successfully. Last inserted ID is: " .$last_id;
$bdd->commit();
in the try.php page i get this error
Undefined index: x AND PDOStatement::execute(): SQLSTATE[23000]: Integrity constraint violation: 1048 Le champ 'code_salle' ne peut être vide (null)
but it's added to the data base i just can't use it in the php code(try.php page)
and also the console.log(text) in the JS works and the payload is x: stp0004 or whatver i clicked .....
in network preview there's no error and i can see the echo just fine but once in the page i get the errors
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
