Jason Jason - 5 months ago 15
Javascript Question

dropdown selection without clicking the submit button

I don't know where to start, if there is any similar question like mine I would be happy to get some idea! Basically the question says it all, Here is what I would like:

I have a simple dropdown menu. When a selection is made in that drop down menu, a php mysql query is ran where the database will be updated with that value. I have all the pieces, all I need is the code that would be able to kick it all off.

For instance when you hit submit on a form you would typically type out:

if (isset($_POST['submit']))

//grab information and insert into db


How would I do this for a drop down selection without having to click the submit button.


you should be looking for something like this

<form id="testform">
<select id="yourselect" name="yourselect" onChange="updateDb()">
 <option value="somevalue">Please select</option>
 <option value="somevalue1">Something</option>

you Javascript function will look like this

function updateDb() {
// I am using jquery for ajax
 $.post("yourserverhandle.php", $("#testform").serialize());

and this is how your "yourserverhandle.php" looks like

$query = "update yourtable set something='".mysql_escape_string($_POST["yourselect"])."' where id='something'";
.... mysql connect, execute