0

i want to use onChange() in select but all of this commands not work for me:

HTML

<form method="post" name='testBank' id="testBank" enctype="multipart/form-data">
<select name="major_id" id='major_id' onchange="this.submit();">
        <option value="">---</option>
        <?
        $query = mysql_query("select * from at_majors where view='1'");
        while($row=mysql_fetch_assoc($query)){
            ?><option value="<? echo $row['major_id']?>" > TEST </option><?
            }
        ?>
    </select>
    </form>

commands

onchange="this.submit()"
onchange="testBank.submit();"
onchange="this.testBank.submit()"
onchange="submit()"
onchange="document.all.submit()"
onchange="document.testBank.submit();"

i get this error:

this.submit is not a function

important

i dont like to use jquery or javascript for this method. please help me to repair this problem. thanks

DolDurma
  • 15,753
  • 51
  • 198
  • 377

5 Answers5

2

That's because you are trying to submit the select tag, not the form.

<form method="post" name='testBank' id="testBank" enctype="multipart/form-data">
<select name="major_id" id='major_id' onchange="document.getElementById('testBank').submit();">

This should work.

EDIT

This should also work:

<select name="major_id" id='major_id' onchange="this.form.submit();">
Community
  • 1
  • 1
Ionuț Staicu
  • 21,360
  • 11
  • 51
  • 58
1

try this

<select name="major_id" id='major_id' onchange=this.form.submit();">
1

So here's your form (simplified a bit for the purposes of the demo):

<form name='testBank'>
<select name="major_id" onchange="submitTestBank();">
        <option value="">---</option>
        <option value="a">a</option>
        <option value="b">b</option>
</select>
</form>

And then the most simple JavaScript (referring to the form by its name):

function submitTestBank() {
    document.forms.testBank.submit();
}

Of course you could just add document.forms.testBank.submit(); directly to the onchange attribute. Having a separate function like submitTestBank is more reusable though. It helps if you want to attach a submit handler to multiple select or input elements.

See it in action.

Oleg
  • 9,341
  • 2
  • 43
  • 58
  • `submit is not a function` – DolDurma Jan 31 '13 at 09:36
  • @TuxWorld Have you tried the link I provided? There's no error. You might be interested in: http://stackoverflow.com/questions/833032/submit-is-not-a-function-in-javascript – Oleg Jan 31 '13 at 09:38
1

If the submit input element have name as "submit" the form won't submit. So please remove the submit.

Avoid this

<input type="submit" name="submit" value="submit">

Use This

<input type="submit" name="xxx" value="xxx">
sakthivel
  • 86
  • 1
  • 1
  • 9
0

You do not like to use jQuery, then try this:

<select name="major_id" id='major_id' onchange="doSomething(this)">

Make sur to define your javascript before the select element:

function doSomething(sel) {
    alert(sel.options[sel.selectedIndex].value);
}
adis
  • 5,901
  • 7
  • 51
  • 71