Page 1 of 1
Dropdown list in PHP form
Posted: Sat Aug 21, 2010 12:03 pm
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>";
Re: Dropdown list in PHP form
Posted: Sat Aug 21, 2010 1:12 pm
by ryanjones102
just an idea:
echo "<select name=regimentnumber onchange=\"this.form.submit()\" value=''>Student Name</option>";
??
Re: Dropdown list in PHP form
Posted: Sat Aug 21, 2010 1:16 pm
by oscardog
You must use double quotes and not single quotes for assigning values to HTML parameters.
So you can't use:
You must use
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.
Re: Dropdown list in PHP form
Posted: Sat Aug 21, 2010 1:28 pm
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...
Re: Dropdown list in PHP form
Posted: Sat Aug 21, 2010 1:58 pm
by cjackson111
Thanks so much for your help. However, that didn't seem to work. I am still having to click the submit button.
Re: Dropdown list in PHP form
Posted: Sat Aug 21, 2010 3:01 pm
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?
Re: Dropdown list in PHP form
Posted: Sat Aug 21, 2010 5:37 pm
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.