I cannot figure out where I'm going wrong here. I have an ajax call that calls an insert statement and returns the ID of that insert. This works.
But I'm trying to create a global variable of this so that I can use the ID as a value in another insert and update that are called in a different ajax call.
I currently get the page ID returned and can echo it in the console, but when I call the 2nd function it says invalid integer type for page_id.
The first ajax call:
<script type="text/javascript">
$(document).ready(function(){
var page_id;
$("#submitForm").click(function(){
event.preventDefault();
var string = $('#pageForm').serialize();
// AJAX Code To Submit Form.
$.ajax({
type: "POST",
url: "addPage.php",
data: string,
dataType: 'json',
cache: false,
success: function(response){
console.log(response['last_insert_id']);
page_id = JSON.stringify(response['last_insert_id']);
}
});
});
});
</script>
calls addpage.php
$addpage = "
INSERT INTO pages (title, page_type_id, display_id, duration)
VALUES ('$title','$page_type','$display_id','$duration');
";
if ($mysqlConn->query($addpage) === TRUE) {
$last_id = $mysqlConn->insert_id;
$data['last_insert_id'] = $last_id;
echo json_encode($data);
} else {
echo "Error: " . $addpage . "<br>" . $mysqlConn->error;
}
So now I want to store the 'last_insert_id' as a global variable back in my first file to use in another ajax call/insert
<script type="text/javascript">
$(document).ready(function(){
$("#form-data").submit(function(e){
var content = tinymce.get("mytextarea").getContent();
$(".leftContent").html(content);
$("#new").val(content);
var string = $('#form-data').serialize() + page_id;
// AJAX Code To Submit Form.
$.ajax({
type: "POST",
url: "addPanel.php",
data: string,
cache: false,
success: function(response){
console.log(JSON.stringify(response));
}
});
return false;
});
});
</script>