bravismore
asked on
HTML Form and php
Anyone with code to populate options for a select box on an HTML form from a mysql database using php.
something like the following:
<form.....>
<select name=".." >
#options should be updated dynamically
<option value= .. </option>
</select>
</form>
something like the following:
<form.....>
<select name=".." >
#options should be updated dynamically
<option value= .. </option>
</select>
</form>
After submission the ID of selected person is stored in variable $person.
ASKER
thanks will try it out
ASKER
It's working but I want it to store multiple values rather than a single value
I want it to store all values selected
I want it to store all values selected
OK. That's easy:
<select name="person[]" size="5" multiple>
<?
$res = mysql_query("select * from person order by lastname,firstname");
if($inf = mysql_fetch_array($res))
{
do{
echo "<option value=\"".$inf["ID"]."\">" .$inf["fir stname"]." ".$inf["lastname"]."</opti on>";
} while($inf = mysql_fetch_array($res));
}
?>
</select>
After submission you have an array called $person. You could loop through this array to get all your options:
for($no=0;$no<count($perso n);$no++)
{
echo $person[$no];
}
Note the change in <select> :
<select name="person[]" size="5" multiple>
<select name="person[]" size="5" multiple>
<?
$res = mysql_query("select * from person order by lastname,firstname");
if($inf = mysql_fetch_array($res))
{
do{
echo "<option value=\"".$inf["ID"]."\">"
} while($inf = mysql_fetch_array($res));
}
?>
</select>
After submission you have an array called $person. You could loop through this array to get all your options:
for($no=0;$no<count($perso
{
echo $person[$no];
}
Note the change in <select> :
<select name="person[]" size="5" multiple>
ASKER
Thanks Will give it a try.
I want a user to select a particular option and would want to make reference to the selected value. I would like to pass this value to another page for processing.
What should i write for
form action and method.
I want a user to select a particular option and would want to make reference to the selected value. I would like to pass this value to another page for processing.
What should i write for
form action and method.
method should always be post. Action would be the page you want the page to be submitted to.
<form action="otherPage.html" method="post">
<form action="otherPage.html" method="post">
ASKER
Sorry, I want it as a drop down box.
With mutiple values
I want a data-driven drop down box.
With mutiple values
I want a data-driven drop down box.
Drop-down box where you could select multiple values is not possible in HTML.
Batalf.
Batalf.
ASKER
Sorry I want to select a single value from multiple values
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Accept: batalf
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Sam Barnum
EE Cleanup Volunteer
I will leave a recommendation in the Cleanup topic area that this question is:
Accept: batalf
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Sam Barnum
EE Cleanup Volunteer
ID int
firstname varchar(64)
lastname varchar(64)
Do simply like this:
<select name="person">
<?
$res = mysql_query("select * from person order by lastname,firstname");
if($inf = mysql_fetch_array($res))
{
do{
echo "<option value=\"".$inf["ID"]."\">"
} while($inf = mysql_fetch_array($res));
}
?>
</select>