Jump to content

[SOLVED] Getting POST data from multiple select fields


MmmVomit

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>

  • 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

Archived

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

×
×
  • 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.