Dropdown list in PHP form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cjackson111
Forum Newbie
Posts: 9
Joined: Sat Jul 04, 2009 6:44 am

Dropdown list in PHP form

Post by cjackson111 »

Hello. I have a php form with dromdown lists that are dynamically populated with data. Everything is working as it should except for one thing that I want it to do. When an item is selected from a dropdown, I want it to automatically submit (rather than clicking the submit button). Normally this wouldn't be a problem using onchange='this.form.submit()', but for some reason it is not working. The form is written within a block of PHP code (not plain HTML). My code is below. Not sure what I am doing wrong. Any suggestions would be most appreciated. Thanks!


echo "<select name=regimentnumber onchange='this.form.submit()' value=''>Student Name</option>";

echo "<option value='unknown' selected>Unknown</option>";
while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[regimentnumber]>$nt[regimentnumber]</option>";

}
echo "</select>";
ryanjones102
Forum Newbie
Posts: 5
Joined: Sat Aug 21, 2010 12:49 pm

Re: Dropdown list in PHP form

Post by ryanjones102 »

just an idea:

echo "<select name=regimentnumber onchange=\"this.form.submit()\" value=''>Student Name</option>";

??
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Dropdown list in PHP form

Post by oscardog »

You must use double quotes and not single quotes for assigning values to HTML parameters.

So you can't use:

Code: Select all

<select name='thename'>
You must use

Code: Select all

<select name="thename">
And your next question will most likely be how to use double quotes in an echo. So to output a double quote use a backslash(\). Example:

Code: Select all

echo "<select name=\"regimentnumber\" onchange=\"this.form.submit()\">"; //Fixed this for you. No idea what you were doing with a closing option tag or a value parameter on a select statement.

echo "<option value=\"unknown\" selected=\"selected\">Unknown</option>";
while($nt=mysql_fetch_array($result)){
echo "<option value=\"" . $nt[regimentnumber] . "\">" . $nt[regimentnumber] . "</option>";

}
echo "</select>";
That should now work. However I have not tested it.
ryanjones102
Forum Newbie
Posts: 5
Joined: Sat Aug 21, 2010 12:49 pm

Re: Dropdown list in PHP form

Post by ryanjones102 »

That's two of us... I believe that's a consensus! haha but thanks for cleaning the other bits up... I didn't really read on...
cjackson111
Forum Newbie
Posts: 9
Joined: Sat Jul 04, 2009 6:44 am

Re: Dropdown list in PHP form

Post by cjackson111 »

Thanks so much for your help. However, that didn't seem to work. I am still having to click the submit button.
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Dropdown list in PHP form

Post by oscardog »

cjackson111 wrote:Thanks so much for your help. However, that didn't seem to work. I am still having to click the submit button.
Maybe you could provide a link to the page so I can view it and debug it?
cjackson111
Forum Newbie
Posts: 9
Joined: Sat Jul 04, 2009 6:44 am

Re: Dropdown list in PHP form

Post by cjackson111 »

Unfortunately, the page is up on my testing server and not accessible from the outside. I think you pointed me in the right direction though. I will play around with my quotes. I usually get confused over that anyway. Thanks.
Post Reply