Techno World Inc - The Best Technical Encyclopedia Online!

THE TECHNO CLUB [ TECHNOWORLDINC.COM ] => PHP => Topic started by: Hellraiser on August 29, 2007, 10:53:13 AM



Title: PHP: Multiple select statements - HTML Forms and MySQL
Post by: Hellraiser on August 29, 2007, 10:53:13 AM
Multiple select statements - HTML Forms and MySQL

Lets cut to the chase, how would you pass values from one form to the other using a list with multiple choices? The answer is by adding a square bracket in the name of the select statement

Example:

Code:
<form name="myform" method="post" action="send.php">
<select name="boyfriend[]" style="width:190px" multiple>
<option value"">Select</option>
<option value="caring">Caring</option>
<option value="loving">Loving</option>
<option value="heartless">Heartless</option>
<option value="worthless">Worthless</option>
<option value="timepass">Timepass</option>
<option value="life">Life</option>
</select>
</form>

My boyfriend is my "life", he is "caring" but "heartless" at times, so I select those three options

Now on the send.php page to extract the values of "boyfriend" select statement do the following:

Code:
$boyfriend=$_POST["boyfriend"];

$string="";
for($i=0;$i<sizeof($boyfriend);$i++){
$string .= $boyfriend[$i]." ";}
echo $string; //Display life, caring, heartless

Now if you want to store the value in a MySQL table use a simple insert statement:
Code:
mysql_query("insert into tablename values ('$string')");