Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: on select change, post form to self

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

like image 266
iamjonesy Avatar asked Oct 20 '10 09:10

iamjonesy


2 Answers

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>
like image 162
acm Avatar answered Sep 22 '22 20:09

acm


a quick fix is this:

Add an onchange attribute to your selectlist

<select name="bmsid" onchange="javascript: form.submit();">
like image 28
PHPology Avatar answered Sep 20 '22 20:09

PHPology