PHP: when choosing changes, the mail form for yourself

This is basically what the name says. I have a form with the selected control that I want to make the form send back to itself when it changes.

$bmsclientlist = $clientobj->getBMSClientList(); echo '<form name="changebmsid" method="post" action="' . $_SERVER['PHP_SELF'] . '"><select name="bmsid">'; foreach($bmsclientlist as $bmsclient) { $var = ''; if($client['bmsid'] == $bmsclient['id']) { $var = ' selected="selected"'; } echo '<option value="' . $bmsclient['id'] .'"'. $var .'>' .$bmsclient['clientname'] . '</option>'; } echo '</select></form>'; $backupobj = new AdminBackup(); if(isset($_POST['bmsid']){ $statusarray = $backupobj->getStatusTotalsbyId($_POST['bmsid']); }else{ $statusarray = $backupobj->getStatusTotals(); } 

I know this will attract some javascript, but I'm not too sure how to achieve this.

Any help is most appreciated!

Thanks,

Jonesi

+6
javascript php forms postback
source share
3 answers

This is the <select> that will submit the parent form

 <form method="post" action="#" name="myform"> <select name="x" onchange="myform.submit();"> <option value="y">y</option> <option value="z">z</option> </select> </form> 

All you have to do is give your <form> name and add an onchange event to your <select> ...


Adam is right. Although the example above works fine, I would do it like this:

Using jQuery , but there are many other options available ...

 <head> <script type="text/javascript" src="jquery.js"></script> <script> $(document).ready(function(){ $('#mySelect').change(function(){ myform.submit(); }); }); </script> </head> 

and form

 <body> <form method="post" action="" name="myform"> <select name="x" id="mySelect"> <option value="y">y</option> <option value="z">z</option> </select> </form> </body> 
+12
source share

Quick fix:

Add onchange attribute to your favorites list

 <select name="bmsid" onchange="javascript: form.submit();"> 
+7
source share

Change your selection menu to the following:

 <select name="bmsid" onchange="document.forms['changebmsid'].submit()"> 

Update

Here is another possible approach:

 <script type="text/javascript"> window.onload = function() { document.forms['myform'].addEventListener('change', function() { this.submit(); }, true); }; </script> 

Put it somewhere on your page and you can leave the form as is.

+2
source share

All Articles