drewrockshard
asked on
MySQL update on dropdown "onchange"
Is there a way to run an update query (or even a PHP function) when a drop down menu changes state, such as "onchange"? Basically I have a function that generates the drop down menu, which holds years. If I want to change this year, so something else, I will need it to update a table in the database (due to how my code works). So, I'm wanting the user to be able to select a difference year from the drop down and "onchange" (when they change to a different year), I want another PHP function to be called to update a table to note that we are using the difference year. So, all I'm asking in this questions is if there is a way to initiate a PHP function on an onchange drop down event?
Thanks in advanced.
Thanks in advanced.
ASKER
How would I call this (or place PHP code in this:
this.form.submit()
this.form.submit()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here's the Ajax code you need "to initiate a PHP function on an onchange drop down event."
The steps:
The steps:
- Place the Ajax code, below, preferably, inside the <head></head> tags of your main PHP/HTML page, the action page (action-page.php, for example),
- Attach a proper onchange handler on your desired select element and call sendRequest() within it,
- Pass proper parameters to sendRequest() inside the onchange handler. The first parameter is the path to the server page URL, where the function you want to initiate may be contained (server-page.php, for example). The last parameter is the query string that you want to send to your server page; this parameter is optional and you may choose not to provide any query string.
- Prepare your PHP page so that your desired function will be "initiated" when it got called by the Ajax code, just like when the PHP page is loaded by a user with a Web Browser.
<!--*** action-page.php ***-->
<html>
<head>
<script type="text/javascript"> // This is the basic Ajax function that you can use to "initiate a PHP function...."
function sendRequest(serverPageURL,queryString){ // queryString is optional and can begin with or without the question mark (?)
// Create the XMLHttpRequest Object
var XMLHttp=("XMLHttpRequest" in window)?(new XMLHttpRequest()):(("ActiveXObject" in window)?(new ActiveXObject("Microsoft.XMLHTTP")):null);
if (XMLHttp==null){ // If not available, exit and alert the user
return alert("Your browser doesn't support HTTP requests; please upgrade it.");
}
XMLHttp.onreadystatechange=
function(){
if (XMLHttp.readyState==4 || XMLHttp.readyState=="complete"){
// Do something when the request is complete
if (XMLHttp.status==200 || XMLHttp.statusText=="OK"){
// Do something if the request IS successfully completed
}
else{
// Do something if the request is NOT successfully completed
}
}
else{
// Do something while the request is in progress (being sent)
}
};
XMLHttp.open("GET",serverPageURL+(queryString==null?"":(queryString.charAt(0)!="?" ? "?":"")+queryString),true); // "Open" the request and append the queryString, if specified, to be sent to serverPageURL
XMLHttp.send(null); // Send nothing more since it's a "GET" request
}
</script>
</head>
<body>
<select onchange="sendRequest( 'server-page.php' , 'thevalue='+this.options[this.selectedIndex].value );">
<option value="...">...</option>
<option value="...">...</option>
<option value="...">...</option>
</select>
</body>
</html>
<!--*** server-page.php ***-->
<?php
$value=$_GET["thevalue"]; // Get the value from the Ajax call done in action-page.php
function initiateThisFunction(){
// Do something
}
if ($value != ""){ // If value is not blank,
initiateThisFunction(); // then call initiateThisFunction()
}
?>
1/ call form submit in onchange: <select onchange="this.form.submit
and in php submit handler, update the table
2/ use ajax to update without rereshing:
<select onchange="setParam(this.va
function setParam(myval)
{
ajax call..
}
more about ajax: http://en.wikipedia.org/wiki/Ajax_(programming)
tutorial: http://www.xul.fr/en-xml-ajax.html