Errors:
Notice: Undefined index: id in C:wamp64wwwappForm_Edit_Appt.php on line 16 Notice: Undefined index: id in C:wamp64wwwappForm_Edit_Appt.php on line 33
Line 16 is where I define: $ID = $_POST[‘id’]; Line 33 is the SELECT where I try to use $ID = $_POST[‘id’] in the select instead of calling the new variable Line 38 is where I use the variable I se on line 16
I’m using AJAX from my index file to pass an ID over to my PHP form, which I then use in a select statement to generate some of the content. Whenever I try to get the variable, I get an undefined index error. I’ve read through the other related questions and tried defining the data type as JSON, tried several syntax changes but just keep getting this. Here’s my script to load the form, followed by ajax sending the data(I’ve tried swapping the order of these to no avail):
$('a.edit_appt').on("click", function() { $('.appt_menu').addClass('hidden'); var id = event.id; $('#modalwindow').load('Form_Edit_Appt.php').addClass('show'); $('.backdropper').addClass('show'); $.ajax({ url: 'Form_Edit_Appt.php', type: 'POST', dataType: 'json', data: { 'id': id }, success: function(data) { calendar.fullCalendar('refetchEvents'); alert("success"); } }); });
I’ve also tried adding an alert of id to check it’s correct (it is) and defining the id as a new variable as well, but no difference.
Here’s my call to the data and my usage:
In my first select statement I call the id at the end but call it directly from the post, in the second I call it from my variable. two methods, the same outcome. I also tried an id isset function but no luck.
$ID = $_POST['id']; $apquery = " SELECT id, events.PersonID, CONCAT('(ID: ',events.PersonID,') ',ForeName,' ',SurName) AS 'patient', CAST(start_event AS date), CAST(end_event AS date), CAST(start_event AS time), CAST(end_event AS time), appt_status, Appt_Type, background_color, notes, events.Appt_Type_ID, events.appt_status_ID FROM `events` INNER JOIN tbl_appt_status AS status ON events.appt_status_ID = status.Appt_status_ID INNER JOIN tbl_appt_types AS type ON events.Appt_Type_ID = type.Appt_Type_ID INNER JOIN tbl_contacts AS contact ON events.PersonID = contact.PersonID WHERE id='".$_POST['id']."' "; $result11 = mysqli_query($connect, $apquery); $apquerytwo = " SELECT id, events.PersonID, CONCAT('(ID: ',events.PersonID,') ',ForeName,' ',SurName) AS 'patient', CAST(start_event AS date), CAST(end_event AS date), CAST(start_event AS time), CAST(end_event AS time), appt_status, Appt_Type, background_color, notes, events.Appt_Type_ID, events.appt_status_ID FROM `events` INNER JOIN tbl_appt_status AS status ON events.appt_status_ID = status.Appt_status_ID INNER JOIN tbl_appt_types AS type ON events.Appt_Type_ID = type.Appt_Type_ID INNER JOIN tbl_contacts AS contact ON events.PersonID = contact.PersonID WHERE id=.$id. "; $result12 = mysqli_query($connect, $apquerytwo);
Advertisement
Answer
You’re making two AJAX requests. The first comes from .load()
(which is just a shortcut for a call to $.ajax()
which writes the response HTML into the specified element), which doesn’t have any POST parameters; the second comes from $.ajax()
, which does.
You should do a single AJAX request that does everything you want. You can provide a data parameter to .load()
, and also a callback function.
$('#modalwindow').load('Form_Edit_Appt.php', {id: id}, function() { $("#modalwindow, .backdropper").addClass("show"); calendar.fullCalendar('refetchEvents'); });