-1

Errors:

Notice: Undefined index: id in C:\wamp64\www\app\Form_Edit_Appt.php on line 16 Notice: Undefined index: id in C:\wamp64\www\app\Form_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);
Barmar
  • 741,623
  • 53
  • 500
  • 612
Greg
  • 21
  • 3
  • 2
    **WARNING**: When using `mysqli` you should be using [parameterized queries](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and [`bind_param`](http://php.net/manual/en/mysqli-stmt.bind-param.php) to add any data to your query. **DO NOT** use string interpolation or concatenation to accomplish this because you have created a severe [SQL injection bug](http://bobby-tables.com/). **NEVER** put `$_POST`, `$_GET` or data *of any kind* directly into a query, it can be very harmful if someone seeks to exploit your mistake. – tadman Jun 24 '19 at 23:58
  • Note: The [object-oriented interface to `mysqli`](https://www.php.net/manual/en/mysqli.quickstart.connections.php) is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete `mysql_query` interface where missing a single `i` can cause trouble. Example: `$db = new mysqli(…)` and `$db->prepare("…")` The procedural interface is largely an artifact from the PHP 4 era when `mysqli` API was introduced and should not be used in new code. – tadman Jun 24 '19 at 23:58
  • Tip: Instead of defining SQL in a throw-away variable like `$sql6` that could easily be mistaken for another, supply the query string *directly* to the database function in question. Finding a bug caused by accidentally running the wrong query is not fun. – tadman Jun 24 '19 at 23:59
  • 1
    One issue (probably not the main problem) is with your second query. You have [WHERE id=.$id.], not $ID, and I think you can use id='$ID' Otherwise you'll get and Undefined variable: id. Maybe just a typo ? Look at the console and see if the POST is passing "id", and what did isset() show an the PHP page. – SScotti Jun 25 '19 at 00:12
  • "Whenever I try to get the variable, I get an undefined index error", you did not post the exact error (always post the entire exact error) Mostly this means there's no ['id'] in your $_GET array. Always use "isset($VAR['xxx'])" on anything that may not actually be set. ./u/sscotti is correct that your 2nd attempt is malformed as you should not have the dots for concatenation in the query if you don't end the string with double quotes first and of course your variables are case sensitive and you used it in UC the 2nd time but LC the first time so they don't match. – TheSatinKnight Jun 25 '19 at 01:26
  • `$('#modalwindow').load('Form_Edit_Appt.php').addClass('show');` doesn't send any POST parameters when calling the script. – Barmar Jun 25 '19 at 01:31
  • @barmar I call the ajax just below it, it's all part of the same onclick event. I've edited my question to expand that script snipet fully. – Greg Jun 25 '19 at 18:08
  • They're part of the same onclick event, but they're two separate AJAX requests. The first one doesn't send any parameters. – Barmar Jun 25 '19 at 18:10
  • @Barmar the answer you've added only refers to calling the variable on the second php file, not amending the ajax, thus isn't the answer to the question. How would I ajax at the same time, I've tried: $('#modalwindow').load('Form_Edit_Appt.php').addClass('show').ajax({..... – Greg Jun 26 '19 at 15:49
  • I didn't post an answer. You can put post data in the `.load()` call: `$("#modalwindow.load").load("Form_Edit_Appt.php", {id: id}, function(data) { calendar.fullCalendar('refetchEvents'); alert("success"); }).addClass("show");` – Barmar Jun 26 '19 at 17:23
  • @Barmar Sorry! I misread the update at the top and thought i said you'd flagged it as already answered for some reason :S I'm not able to get rid of that but it is wrong! your code stopped the modal opening, and didn't givea console error, but I changed the code to his and it worked perfecly! $('#modalwindow').load("Form_Edit_Appt.php", {id: id}, function(data) { calendar.fullCalendar('refetchEvents');}); $('.backdropper, #modalwindow').addClass('show'); – Greg Jun 26 '19 at 18:19
  • It wasn't me, it was @JayBlanchard. A number people use that duplicate link whenever someone posts a question involving "Undefined index". If you search through the question, you'll see some answers that advise on how to debug issues with `$_POST`. – Barmar Jun 26 '19 at 18:24
  • @Barmar if you want to post your comment as an answer I'd be happy to mark it as the correct one – Greg Jun 26 '19 at 19:01

2 Answers2

0

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');
});
Barmar
  • 741,623
  • 53
  • 500
  • 612
-2

"Whenever I try to get the variable, I get an undefined index error", you did not post the exact error (always post the entire exact error) Mostly this means there's no ['id'] in your $_POST array.

In this case you should also use mysqli_real_escape_string() to clean any post/get data before submission and/or go all the way and submit data as parameters to stored functions in mysql for safety against mysql injection.

Always use "isset($VAR['xxx'])" on anything that may not actually be set.

/u/sscotti is correct that your 2nd attempt is malformed as you should not have the dots for concatenation in the query if you don't end the string with double quotes first and of course your variables are case sensitive and you used it in UC the 2nd time but LC the first time so they don't match.

TheSatinKnight
  • 696
  • 7
  • 16