8

I am trying to not have a submit button, is there any way that the form can submit when the user selects a value?

<form method="post" action="<?php echo $_SERVER['PHP_SELF']  ?>" >

    <table class="form">

            <select name="category" class="formfield" id="category">
                <option value="-1"> Category </option>
                <?php
                    $sql_contry = "SELECT * FROM category";
                    $rs_c = mysql_query($sql_contry);
                    while ($row_c = mysql_fetch_array($rs_c)) {
                        echo '<option value="'.$row_c['category'].'">'.$row_c['category'].'</option>';  
                    }
                ?>
             </select>

    </table>

</form>
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
Danny Boy
  • 139
  • 1
  • 2
  • 8

4 Answers4

29

Here an example

<form>
    <select name='myfield' onchange='this.form.submit()'>
      <option selected>Milk</option>
      <option>Coffee</option>
      <option>Tea</option>
    </select>
    <noscript><input type="submit" value="Submit"></noscript>
</form>

Using noscript tag it allows browsers that are not JavaScript-enabled to still function.

TheEwook
  • 11,037
  • 6
  • 36
  • 55
  • who the hell uses `noscript` nowdays and why is this relevant here ? – Adidi Apr 26 '13 at 20:08
  • @Adidi The content inside the – TheEwook Apr 26 '13 at 20:10
  • ohhhh what do you say ?! I am just saying that in today internet if someone will disable js he could not surf on 90% of the web - the noscript method was relevant to the web 1.0 world... – Adidi Apr 26 '13 at 20:13
  • I have tried all of the solutions that everyone has posted, and it isn't quite working yet, but I think it is close. Normally, when I select a value and click the submit button, it displays data on the screen. With this method, I can tell that it is submitting, but the data is not being displayed on the screen. Any thoughts? That is, without going through 500 lines of code? ;) – Danny Boy Apr 26 '13 at 21:27
  • Awsome find! Been Googling around for hours. – Taavi Jan 18 '21 at 20:06
4

Yes - use javascript:

Html:

<form id="frm">
   <select onchange="onSelectChange();">
          <option>1</option>
           <option>1</option>
   <select>
</form>

js:

function onSelectChange(){
 document.getElementById('frm').submit();
}
Adidi
  • 5,097
  • 4
  • 23
  • 30
  • 1
    You actually need to make the js function take an argument or something to make it actually useful. You can't add 2 select lists with this :) . +1 – AKS Apr 26 '13 at 19:59
  • 1
    Yeah - just my cents in case OP finds it confusing. – AKS Apr 26 '13 at 20:06
2
<form action="product.php" method="POST">
<select onchange="this.form.submit();" name="prod">
    <option value="">Select product</option>
    <option value="1">abc</option>
    <option value="2">def</option>
    <option value="3">ghi</option>
    <option value="4">jkl</option>
    <option value="5">mno</option>
</select>

Abid Shah
  • 325
  • 3
  • 5
0

Just change code as below, I haven't check code so there may be some error like capital/small letter I am not sure like, it's submit() or Submit() and so on..

<script language="javascript">
function submitForm(){
    var val = document.myform.category.value;
    if(val!=-1){
        document.myform.submit();
    }
}
</script>
<form method="post" name="myform" action="<?php echo $_SERVER['PHP_SELF']  ?>" >

    <table class="form">

            <select name="category" class="formfield" id="category" onchange="submitForm();">
                <option value="-1"> Category </option>
                <?php
                    $sql_contry = "SELECT * FROM category";
                    $rs_c = mysql_query($sql_contry);
                    while ($row_c = mysql_fetch_array($rs_c)) {
                        echo '<option value="'.$row_c['category'].'">'.$row_c['category'].'</option>';  
                    }
                ?>
             </select>

    </table>

</form>
web2students.com
  • 307
  • 2
  • 16
  • Thank you, but I copied and pasted the code, and for some reason it does not submit. I looked for capital / small letter errors but could not find any. Any thoughts? – Danny Boy Apr 27 '13 at 22:37