Adapted from this question: Passing Jquery variable to php within a modal
I have it set up, and it should be working, but I am getting the undefined index error.
Inside a while loop:
print '<li>';
print '<a class="btn btn-default" data-toggle="modal" data-id='.$row['id'].' data-target=".bs-example-modal-lg"><img src="'.$row["image"].'" /></a><br>';
print '<a class="btn btn-default" data-toggle="modal" data-id='.$row['id'].' data-target=".bs-example-modal-lg"><h4>'.$row['name'].'</h4></a>';
print '</li>';
All of my jquery, including the ajax code:
$(document).ready(function() {
$("body").css("display", "none");
$("body").fadeIn(1000);
$("a.transition").click(function(event){
event.preventDefault();
linkLocation = this.href;
$("body").fadeOut(1000, redirectPage);
});
function redirectPage() {
window.location = linkLocation;
}
});
$(document).on("click", ".btn", function (e) {
e.preventDefault();
alert (id);
var id = $(this).data('id');
alert (id);
$("h4.modal-title").text(id);
//$($(this).attr('href')).modal('show');
$.post('food.php',{id:id},function(data){
alert(data);
alert (id) // <--THIS ALERT WORKS TOO
});
});
The php:
<?php
$id = $_POST["id"];
print '<h4>'.$id.'</h4>'
?>
I keep getting an index undefined error on $id = $_POST["id"];
.
I cant seem to figure out why, I have adapted my code to so many different ways of doing it, but none of them work.
The first alert shows up, but the second does not. The text does get placed within the <div>
, however it does not get sent to the variable. All of this is in one file.
I have a feeling my ajax code is missing something.
A Var dump shows:
array (size=0)
empty