PHPJavascript

Web Development Guide – My Personal Library of Tutorials and Scripts

Multiple select statements – HTML Forms and MySQL

August4

Extract values from multiple select statements or list with multiple choices for HTML forms and MySQL tables using PHP.

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:

<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:

$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:
mysql_query(”insert into tablename values (’$string’)”);

posted under MySQL, PHP

Email will not be published

Website example

Your Comment:

 

2,885 spam comments
blocked by
Akismet