Jump to content

Archived

This topic is now archived and is closed to further replies.

dude753

Drop down menus with multiple selection

Recommended Posts

Howdy

I have a drop down menu which has multiple selection enabled. I want to put all the options selected into a db seperated by a commar.

So:
Orange|Red

Etc.

Here is my code:
[code]<td width='30%' valign='top'><strong>Access</strong><br><i>To select more than one category hold ctrl then click.</i></td>
      <td valign='top'><select name='access' multiple='multiple' height='$height'>";
while($r=mysql_fetch_array($result)) { // start looping
  $name = $r["name"];
  $id=$r["id"];
 
  echo "<option value='$name'>$name</option>"; // echoing out different categories
  }
    echo "<option value='Admin'>Admin</option></select>
    </td>[/code]

Then, to insert it into the db I have:
[code]else if($_POST['Submit'] == "Update") //if submit is hit
{
mysql_connect("$localhost","$dbuser","$dbpass") or die("error");
mysql_select_db("$dbname") or die(mysql_error());

  //makes incoming data variables
  $username = $_POST['username'];
  $password = $_POST['password'];
  $access = $_POST['access'];
  $id = $_POST['id'];
 
    if($username && $password && $access) {
$password = md5($_POST['password']);
    $result = mysql_query("UPDATE users SET username='$username', password='$password', access='" . join(",",$_POST["access"]) . "' WHERE id='$id'") or die (mysql_error());
}[/code]

For some reason this isn't working at all. It only allows me to select one option on the drop down. Any idea why this is?

I'm usnig Firefox.

Thanks!

Share this post


Link to post
Share on other sites
This will point the way ... save it and test it to see what it does:

[code]<?php
if (isset($_POST['submit'])) {
    $test = $_POST['skill_sets'];
    print_r($test);
}
?>

<html>
<head>
<title>Test</title>
</head>

<body>
<form name="test" method="post">
<select name="skill_sets[]" multiple size="3">
<option value="Academics">Academics</option>
<option value="Math">Math</option>
<option value="Problems">Problem Solving</option>
<option value="Project Management">Project Management</option>
<option value="Basketweaving">Basketweaving</option>
</select>
<input type="submit" name="submit" value="Pick Some"/>
</form>
</body>
</html>[/code]

Share this post


Link to post
Share on other sites
yes... AndyB's method will write all of the selected drop down options into another dimension of the $_POST global so then you can just loop through that $_POST['skill_sets'] array to write all of the selected values do your DB...

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.