10

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>
3
  • Yeah, by using javascript ! Commented Apr 26, 2013 at 19:56
  • You'll have to use Javascript to do that. Commented Apr 26, 2013 at 19:56
  • Use javascript. A similar example is here: stackoverflow.com/questions/10021848/… Commented Apr 26, 2013 at 19:58

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

who the hell uses noscript nowdays and why is this relevant here ?
@Adidi The content inside the <noscript> element will only be displayed if scripts are not supported, or are disabled in the user’s browser.
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...
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? ;)
Awsome find! Been Googling around for hours.
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();
}

2 Comments

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
Yeah - just my cents in case OP finds it confusing.
1
<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>

Comments

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>

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.