Jump to content

Archived

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

Gmunky

how do I get multiple values from a listbox once the form is submitted?

Recommended Posts

here is what I have. when two or more values are selected from the listbox, it only outputs the lowest selected value from the list.

//script1
<form action="script2.php" ?>" method="post">
<select multiple="multiple" name="hrs">
      <option value="10 hours">10 hours</option>
<option value="20 hours">20 hours</option>
<option value="30 hours">30 hours</option>
<option value="40 hours">40 hours</option>
</select>
<input type="submit" name="submit" value="Submit"  />
</form>

//script2
if (isset($_POST['submit']))
{

$hrs=$_POST['hrs'];

echo "hours week: $hrs <br>";
        }

Share this post


Link to post
Share on other sites
Name you checkbox like this

<select multiple="multiple" name="hrs[]">

The multiple options are then posted in an array. Process as
[code]
foreach ($_POST['hrs'] as $value) {
    // do something with value

    echo "$value <br />";
}[/code]

Share this post


Link to post
Share on other sites
You need to make the name of the input field an array:
[code]<?php
if (isset($_POST['submit'])) echo '<pre>' . print_r($_POST,true) . '</pre>';
?>
<html>
<head>
<title>Test Multiple</title>
</head>
<body>
<form method="post">
<select multiple name="hrs[]">
      <option value="10 hours">10 hours</option>
  <option value="20 hours">20 hours</option>
  <option value="30 hours">30 hours</option>
  <option value="40 hours">40 hours</option>
  </select>
<input type="submit" name="submit" value="Submit"  />
</form>
<?php
if (isset($_POST['submit']))
  {

        $hrs=(is_array($_POST['hrs']))?implode(', ',$_POST['hrs']):$_POST['hrs'];

        echo "hours week:" . $hrs;
        }
?>
</body>
</html>[/code]

Notice this line: [color=red]<select multiple [b]name="hrs[]">[/b][/color]

Ken

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.