Select box selecting

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Select box selecting

Post by tommy1987 »

Hi, I have the following code, what I want to do is submit that form when an option is selected, is there some way I can do this?
My reasoning for this is that I wish to populate another select box depending on what this one is.

Code: Select all

<?php
     //Code to display the region select box from the DB
     include("include-mysql.php"); 
     $query = "SELECT regionName from Region ORDER BY regionName ASC";
     $result = mysql_query($query) or die(mysql_error().'<p>'.$query.'</p>');
     $i = 0;
     
     echo '<form name="RegSel" method="get" action="register.php"><select name="Region">';
      
   while ($row = mysql_fetch_array($result)) {
          echo '<option value="'.$row['regionName'].'">'.$row['regionName'].'</option>';
          $i++;
}
  ?>
Thanks very much.. Tom
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Moved to Client Side
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Change your select tag to something like this:

Code: Select all

<select name="blah" onChange="myform.submit();">
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Post by tommy1987 »

Here is my code, it still doesn't work though, the form just will not submit at all!

Code: Select all

<?php
//Code to display the region select box from the DB
    include("include-mysql.php"); 
    $query = "SELECT regionName from Region ORDER BY regionName ASC";
    $result = mysql_query($query) or die(mysql_error().'<p>'.$query.'</p>');
    $i = 0; ?>
    <form name="myform1" method="get" action="register.php"><select onChange="myform1.submit();" name="Region"> <?php
    while ($row = mysql_fetch_array($result)) {
        echo '<option value="'.$row['regionName'].'">'.$row['regionName'].'</option>';
        $i++;
       }
    echo '</select></form>';
?>
Anyone any ideas??

Thanks, Tom
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Are you testing this in Firefox? What Javascript errors (if any) are you getting?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Post by tommy1987 »

it is saying 'myform1' is undefined which we all know is not true!

Im testing in IE but have tried in firefox too of which neither work..

Thanks..
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Maybe try

Code: Select all

document.getElementById('myform1').submit()
rather that

Code: Select all

myform1.submit()
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Post by tommy1987 »

Having done that, I am now getting the following error:

Error: 'document.getElementById[...]' is null or not an object

Its driving me mad!!

Hope someone knows what the problem is, never had any problems with doing this kind of thing before!!

-Tom
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

how about

Code: Select all

this.form.submit();
I hope you don't have another field (or button) named submit though.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Add the id to your form tag

Code: Select all

<form name="myform1" id="myform1" method="get" action="register.php">
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Post by tommy1987 »

Thanks very much got it working with this.form.submit().
Post Reply