8

It's basically what the title says.. I have a form with a select control that I want to force the form to post back to self on change.

$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 it's going to involve some javascript but I'm not too sure how to achieve this.

Any help most appreciated!

Thanks,

Jonesy

Tatu Ulmanen
  • 123,288
  • 34
  • 187
  • 185
iamjonesy
  • 24,732
  • 40
  • 139
  • 206

3 Answers3

14

This is a <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 a name to your <form> and add the onchange event to your <select>...


Adam is right. While the example above works perfectly, 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 the 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>
acm
  • 6,541
  • 3
  • 39
  • 44
  • 2
    Note that while this is the core way to do it, it's not semantic code - the onchange should be abstracted to the document head or an external script using a dom load event such as jquery `$(document).ready` – Adam Hopkinson Oct 20 '10 at 09:14
  • Nice simple example of unobtrusive js. – Joshua Pinter Mar 09 '11 at 16:47
  • In your example, what is the benefit to using jQuery? @adam, would you be able to edit andre's answer to reflect your suggestions? I'm curious how to make this better. – Ryan Edwards Nov 08 '11 at 05:28
  • @RyanEdwards, Adam's comment was mentioning the first part of my answer. The jQuery part already reflects his suggestions with jQuery placed at the document ``. Benefits of using jQuery? You can accomplish the same with less code. – acm Nov 08 '11 at 11:18
  • this.form.submit(); is easier to reuse. – M H Aug 28 '15 at 17:47
8

a quick fix is this:

Add an onchange attribute to your selectlist

<select name="bmsid" onchange="javascript: form.submit();">
PHPology
  • 1,017
  • 6
  • 12
2

Change your select menu to this:

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

Update

Here's another possible approach:

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

Put that anywhere on your page, and you can leave you form as it is.

mellowsoon
  • 22,273
  • 19
  • 57
  • 75