Right now, I have variables that default to the current d/m/y, which is then popped into a mySQL query that displays all data from a table WHERE date=’$dday’ AND month=’$dmonth’ AND year=’$dyear’.
$ddate = date("d");
$dmonth = date("m");
$dyear = date("Y");
Instead, I’d like to have a select box that will change the variables based on the option selected. So the default option for the Day select will be the current date, but if I change that to day 12 for example, I want the variable to change when the select option changes, and then re-query the database automatically. I’m assuming this would be done with AJAX.
Is what I’m talking about even possible? If the automation of the query adds a big layer of complexity, I’d be fine with just changing the variable and updating based on the press of a submit button.
I promise to take a break from asking questions and start answering some, if questions below my simple level are even asked.
Yes, this is possible. jQuery even makes it easy.
Create your
<select>element and give it an ID so it can be used in jQuery.Add an event listener in jQuery that is fired when the
<select>changes, like$("#date").change().In the event handler for the change event, get the current value of the
<select>, and then use jQuery’s AJAX$.post()function to send that data to a PHP file.In that PHP file, sanitize the data to prevent MySQL Injections, and then query the database for the new data.
Use PHP’s
echofunction to send back the data.In the jQuery
$.post()callback function (third parameter), receive the echoed data and put it into a variable.Use jQuery to update your HTML with the data.