how to echo selected=selected from multi-select list

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

holy24
New php-forum User
New php-forum User
Posts: 6
Joined: Mon Sep 03, 2012 7:28 pm

how to echo selected=selected from multi-select list

Postby holy24 » Sun Oct 14, 2012 8:51 pm

Hi guys,

I have created a form with drop-down field and multi-select list field for user to select. However, the application will allow user to edit their data after they submit the form and when they click "edit" button, the form will auto detect what they have input during the form submission.

Below are the codes that I have wrote, however I am stuck in auto detect the data from muti-select list field.

form.php:
<form name="form1" method="post" action="staff.php">
Sex:
<select name="sex">
<option value="male">male</option>
<option value="female">female</option>
</select>

Education:
<select name="education[]" multiple="multiple">
<option value="degree">degree</option>
<option value="diploma">diploma</option>
</select>
</form>

staff.php:
$sex=$_POST['sex'];
$education=$_POST['education'];
$education1= "<p>$education[0]</p> . <p>$education[1]</p>";
mysql_query ("INSERT INTO staff (sex, education) VALUES ('$sex', '$education1')");

edit.php:
$result = mysql_query ("SELECT * FROM staff");
$row = mysql_fetch_array($result);

<form name="form1" method="post" action="update.php">
Sex:
<select name="sex">
<option value="male" <?php if ($row['sex']=="male") echo "selected=selected";?>>male</option>
<option value="female" <?php if ($row['sex']=="female") echo "selected=selected";?>>female</option>
</select>

Education:
<select name="education[]" multiple="multiple">
<option value="degree">degree</option>
<option value="diploma">diploma</option>
</select>

</form>

Question: When user select both "degree and diploma", it will go to a database column called "education", and this is the data that will appear "<p>degree</p> <p>diploma</p>". I am not sure how can I auto detect and select the data when user click edit button?

Please help. Thanks.

Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 1 guest

cron