Solved

HTML Form and php

Posted on 2002-06-19
14
213 Views
Last Modified: 2006-11-17
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>
0
Comment
Question by:bravismore
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
14 Comments
 
LVL 32

Expert Comment

by:Batalf
ID: 7093785
Let's say you have a table called person with these fields:

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"]."\">".$inf["firstname"]." ".$inf["lastname"]."</option>";
  } while($inf = mysql_fetch_array($res));
}
?>
</select>


0
 
LVL 32

Expert Comment

by:Batalf
ID: 7093792
After submission the ID of selected person is stored in variable $person.

0
 

Author Comment

by:bravismore
ID: 7094855
thanks will try it out
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bravismore
ID: 7094947
It's working but I want it to store multiple values rather than a single value

I want it to store all values selected
0
 
LVL 32

Expert Comment

by:Batalf
ID: 7095012
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["firstname"]." ".$inf["lastname"]."</option>";
 } 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($person);$no++)
{
  echo $person[$no];
}


Note the change in <select> :

<select name="person[]" size="5" multiple>



0
 

Author Comment

by:bravismore
ID: 7095242
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.
0
 
LVL 32

Expert Comment

by:Batalf
ID: 7095247
method should always be post. Action would be the page you want the page to be submitted to.

<form action="otherPage.html" method="post">

0
 

Author Comment

by:bravismore
ID: 7095270
Sorry, I want it as a drop down box.
With mutiple values

I want a data-driven drop down box.
0
 
LVL 32

Expert Comment

by:Batalf
ID: 7095274
Drop-down box where you could select multiple values is not possible in HTML.

Batalf.
0
 

Author Comment

by:bravismore
ID: 7095295
Sorry I want to select a single value from multiple values
0
 
LVL 32

Accepted Solution

by:
Batalf earned 60 total points
ID: 7095301
Then, can't you use my first example which includes a drop-down box with several options. From the drop-down you could select a single value:

<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"]."\">".$inf["firstname"]." ".$inf["lastname"]."</option>";
 } while($inf = mysql_fetch_array($res));
}
?>
</select>
0
 
LVL 11

Expert Comment

by:shmert
ID: 9643503
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              
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question