Jump to content

Recommended Posts

If you have a form like this

 

<form method="post" action="foo.php">
  <select name="bar" multiple>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
  </select>
  <input type="submit" name="submit" value="Submit" />
</form>

 

Is there a way to properly retrieve the multiple selected items from the "bar" select box?

 

If multiple options are selected, the $_POST variable only contains the last option selected.  I've written a function myself that solves the problem, but was wondering if there is a way native to PHP, so I don't have to worry about having to put this function in every website I write.

 

You need to make the name of the field an array:

<form method="post" action="foo.php">
  <select name="bar[]" multiple>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
  </select>
  <input type="submit" name="submit" value="Submit" />
</form>

 

Then you will get multiple values returned.

 

Ken

I guess their would be a PHP function for this, I'm guessing your function makes an array out of the multiple values? or something along those lines?

Yep.  If I had the code handy, I'd post it.  It parses the raw post data from the file 'php://input'.

You need to make the name of the field an array:

<form method="post" action="foo.php">
  <select name="bar[]" multiple>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
  </select>
  <input type="submit" name="submit" value="Submit" />
</form>

 

Then you will get multiple values returned.

 

Ken

That works?  Cool.  I'll have to try that.

I don't have PHP on this computer.  Would someone be willing to run the following script and see if it works?

 

Pretty please?  With a cherry on top?

 

<html>
<head>
<title>
Title
</title>
</head>

<body>

<form method="post" action="">
  <select name="bar[]" multiple>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
  </select>
  <input type="submit" name="submit" value="Submit" />
</form>

<pre>
<?php

print_r($_POST);

?>
</pre>

</body>
</html>

Here's the result after selecting two of the options:

Array
(
    [bar] => Array
        (
            [0] => 2
            [1] => 4
        )

    [submit] => Submit
)

 

Ken

  • 6 months later...

You need to make the name of the field an array:

<form method="post" action="foo.php">
  <select name="bar[]" multiple>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
  </select>
  <input type="submit" name="submit" value="Submit" />
</form>

 

Then you will get multiple values returned.

 

Ken

 

 

 

could someone show me what the code would look like to put the data from this array into var's once it's posted?

 

 

 

thanks,

bb

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.