Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
| Search | Today's Posts | Mark Forums Read
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old May 18th, 2004, 11:59 AM
Authorized User
Join Date: Jun 2003
Location: , , .
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
Default drop down value into mysql

Hello there,

I am trying to pass on the value of a dropdown list to mysql table.

I can pass input box variables to mysql, but not dropdown boxes...

this is what my input boxes look like and they work;
Address: <input type=text name=address value="<? echo $address; ?>">

and obviously after user enters the info i query the db to insert value address=$address into db. But I don't seem to know how to figure out the dropdown box. I want the dropdown box to have a value of yes and one of no. So when the user submits their form the value of the dropdown box would be inserted in the oppropriate field in the db.

Your help is much appreciated.
Old May 18th, 2004, 12:33 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts

Are you talking about <select> fields? There is nothing special that you have to do to access the value.

<form action='page.php' method='post'>
    <select name='question'>
        <option value='1'>Yes</option>
        <option value='0'>No</option>

This will be available in page.php as $_POST['question'], and additionally as $question if the php.ini directive register_globals has a value of 'On'.

If this were an <input> text field the variable would be the same.


The Spicy Peanut Project
Old May 19th, 2004, 07:32 AM
Friend of Wrox
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts

From a usablity point of view, may I suggest using a pair of radio buttons, instead? Radio buttons are used in sets, identified by their sharing the same name attribute and only one selection is possible. So you'd have something like:

Would you describe yourself as an Amoeboid Zingat-Ularian?<br />
Yes<input type="radio" name="rad" value="yes" id="radio1">
No<input type="radio" name="rad" value="no" id="radio1">

Whichever value is selected is passed to your script upon submission. The advantage is that the user can see the options available to them without having to interact with the form.

Without digressing too far into a discussion about HTML, you can improve the accessibility further by wrapping them in labels (this is a great help to Amoeboid Zingat-Ularians, who have very short tentacles - as anyone who's met one will tell you - and they thus find using a conventional mouse very difficult). Your radio buttons now look like this:

<legend>Would you describe yourself as an Amoeboid Zingat-Ularian?</legend>
<div><label for="radio1"><input type="radio" name="rad"
value="yes" id="radio1">Yes</label></div>
<div><label for="radio2"><input type="radio" name="rad"
value="no" id="radio2" checked>No</label></div>

Note the use of the 'id' to identify the actual button, and the 'name' attribute to identify the set of controls, as a group. Now our tentically-challenged Amoeboid Zingat-Ularians need only hit the appropriate text, instead of the actual control, to switch the values. The text can be anywhere on the page, as well.

This applies to IE4+, Netscape6+ and all versions of Mozilla Opera 5+, and has no adverse affects for browsers that don't support the <label> element.

Similar Threads
Thread Thread Starter Forum Replies Last Post
drop down list values based on another drop down noor ASP.NET 1.0 and 1.1 Basics 3 July 5th, 2005 09:57 AM
value of the drop-down crmpicco Classic ASP Basics 15 February 24th, 2005 11:11 AM
insert values into a MySQL DB from a drop down men crmpicco Classic ASP Basics 26 January 24th, 2005 08:10 AM
Drop down sinner Classic ASP Databases 2 March 19th, 2004 02:51 PM
Populating Drop Down based on other drop down jeffbarclay Java Databases 1 November 7th, 2003 12:14 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.