Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create and use a combo box in a web page form

Status
Not open for further replies.

mondeoman

MIS
Dec 7, 2006
203
GB
I have a page on my site where the user can add a new record to a table in a database by using a fairly standard form. To ensure data consisitency I want the user to be able to select standard responses in certain fileds. For example one field is called "eventtype". This filed determines whether an event which our charity is working at is a fundraiser, profile raiser or other type of event (e.g. recruitment). The code for my form looks like this:

Code:
<form id="form1" name="form1" method="post" action="">
  <table width="49%" border="0" align="center">
      <tr>
      <td>eventname</td>
      <td><input name="eventname" type="text" id="eventname" size="64"/></td>
    </tr>
    <tr>
      <td>Location</td>
      <td><input name="eventlocation" type="text" id="eventlocation" size="64" /></td>
    </tr>
	   <tr>
      <td>Status</td>
      <td><input name="eventstatus" type="text" id="eventstatus" /></td>
    </tr>
	   <tr>
      <td>Type of Event</td>
	
      <td><input name="eventtype" type="text" id="eventtype" /></td>
    </tr>
    <tr>
      <td>Date</td>
      <td><input name="eventdate" type="Date" id="eventdate" /></td>
    </tr>
	   <tr>
      <td>Start Time</td>
      <td><input name="starttime" type="text" id="starttime" /></td>
    </tr>
	   <tr>
      <td>End Time</td>
      <td><input name="endtime" type="text" id="endtime" /></td>
    </tr>
    <tr>
      <td>Coordinator</td>
      <td><input name="coordinator" type="text" id="coordinator" /></td>
    </tr>
	   <tr>
      <td>Comments</td>
      <td><textarea name="comments" cols="64" id="comments"></textarea></td>
	   </tr>
	   <tr>
      <td>Funds Raised</td>
      <td><input name="fundsraised" type="decimal(5,2)" id="fundsraised" /></td>
    </tr>
	   <tr>
      <td>Financial Year</td>
      <td><input name="financialyear" type="text" id="financialyear" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="Submit" value="Submit" /></td>
    </tr>
  </table>
</form></p></td>  </tr></table></td>  </tr></table>

I want the user to be able to select from a combo box an option value say of "fundraiser", "Profile", "Recruitment" or some other hard coded answer. I want the selection then to populate the fieldd in my database with the selected answer once the Submit button is clicked. The connection and field information is shown below:

Code:
/connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
  or die("Unable to connect to MySQL");
//select a database to work with
$selected = mysql_select_db("eachfrie_Events",$dbhandle)
  or die("Could not select eachfrie_Events");
  
// query db and loop through rows example

$field3	= $_POST['eventname'];
$field4	= $_POST['eventlocation'];
$field5	= $_POST['eventtype'];
$field6	= $_POST['eventstatus'];
$field7	= $_POST['eventdate'];
$field8	= $_POST['starttime'];
$field9	= $_POST['endtime'];
$field10 = $_POST['coordinator'];
$field11 = $_POST['comments'];
$field12 = $_POST['fundsraised'];
$field13 = $_POST['financialyear'];

if($field3){

	 $sql = "
    INSERT INTO
        `tblEvents` (
            `eventname`
            ,`eventlocation`
            ,`eventtype`
            ,`eventstatus`
            ,`eventdate`
            ,`starttime`
            ,`endtime`
            ,`coordinator`
            ,`comments`
            ,`fundsraised`
            ,`financialyear`
        ) VALUES  (
            '".$field3."'
            ,'".$field4."'
            ,'".$field5."'
            ,'".$field6."'
            ,'".$field7."'
            ,'".$field8."'
            ,'".$field9."'
            ,'".$field10."'
            ,'".$field11."'
            ,'".$field12."'
            ,'".$field13."'
        )
    ;
";

echo $sql; //debug line

mysql_query($sql);

Can anyone help me please?
 
Since you are already inserting the value into your DB, all you need to do is change the html <input> to a drop down or <select>.

Code:
<tr>
<td>Type of Event</td>

<td><[red]select [/red] name="eventtype" id="eventtype" />
     <option value="Fundraiser" selected=selected>Fundrasier</option>
     <option value="Profile">Profile</option>
     ...
     </select>
</td>
</tr>
<tr>


The value attribute is what will get sent to your PHP code to be inserted in the DB.

The text between the <option> tags is what will be displayed
they can be the same or different things depending on your needs.
The "selected=selected" attribute lets you preselect an option.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Thank you that was extremely helpful but now I get the Insert statement appearing at the top of my page with the values as shown here:

Code:
INSERT INTO `tblEvents` ( `eventname` ,`eventlocation` ,`eventtype` ,`eventstatus` ,`eventdate` ,`starttime` ,`endtime` ,`coordinator` ,`comments` ,`fundsraised` ,`financialyear` ) VALUES ( 'Some event' ,'Stamford' ,'Profile' ,'Approved' ,'2011-03-12' ,'' ,'' ,'Noone' ,'' ,'' ,'2011' ) ; [code/]

How do I avoind this please?
 
Thats great. Thank you very much for the help - much appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top