PHP Submit on Select
Asked Answered
R

4

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>
Rockandroll answered 26/4, 2013 at 19:55 Comment(3)
Yeah, by using javascript !Illbred
You'll have to use Javascript to do that.Semiotic
Use javascript. A similar example is here: #10022348Wellesley
S
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.

Smoking answered 26/4, 2013 at 19:57 Comment(5)
who the hell uses noscript nowdays and why is this relevant here ?Auden
@Auden The content inside the <noscript> element will only be displayed if scripts are not supported, or are disabled in the user’s browser.Smoking
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...Auden
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? ;)Rockandroll
Awsome find! Been Googling around for hours.Algonquin
A
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();
}
Auden answered 26/4, 2013 at 19:57 Comment(2)
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 :) . +1Liftoff
Yeah - just my cents in case OP finds it confusing.Liftoff
A
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>

Australian answered 20/2, 2019 at 11:58 Comment(0)
K
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>
Keratogenous answered 26/4, 2013 at 20:3 Comment(1)
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?Rockandroll

© 2022 - 2024 — McMap. All rights reserved.