Jump to content


Photo

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


  • Please log in to reply
3 replies to this topic

#1 Gmunky

Gmunky
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 07 September 2006 - 01:37 PM

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



#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 07 September 2006 - 02:05 PM

Name you checkbox like this

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

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

    echo "$value <br />";
}

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 07 September 2006 - 02:09 PM

You need to make the name of the input field an array:
<?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>

Notice this line: <select multiple name="hrs[]">

Ken

#4 Gmunky

Gmunky
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 07 September 2006 - 02:14 PM

thank you!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users