Jump to content

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


Gmunky

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>";
        }

Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.