MySQL update on dropdown "onchange"

drewrockshard
drewrockshard used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
two solutions:
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.value)">

function setParam(myval)
{
  ajax call..
}

more about ajax: http://en.wikipedia.org/wiki/Ajax_(programming)
tutorial: http://www.xul.fr/en-xml-ajax.html

Author

Commented:
How would I call this (or place PHP code in this:

this.form.submit()
that command submits your form, so you just need to code a standard form processing...

<?php
  if ($_POST['year'])
 {
    ... your code to update table...
  }
?>
<form method="post" action="">
<select name="year" onchange="this.form.submit()">
...options...
</select>
</form>

Commented:
Here's the Ajax code you need "to initiate a PHP function on an  onchange drop down  event."

The steps:
  1. 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),
  2. Attach a proper onchange handler on your desired select element and call sendRequest() within it,
  3. 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.
  4. 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.
I hope the code below will satisfy your needs. Good luck.

<!--*** 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()
   }

?>

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial