MmmVomit Posted October 8, 2007 Share Posted October 8, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/72322-solved-getting-post-data-from-multiple-select-fields/ Share on other sites More sharing options...
MasterACE14 Posted October 8, 2007 Share Posted October 8, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/72322-solved-getting-post-data-from-multiple-select-fields/#findComment-364673 Share on other sites More sharing options...
kenrbnsn Posted October 8, 2007 Share Posted October 8, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/72322-solved-getting-post-data-from-multiple-select-fields/#findComment-364679 Share on other sites More sharing options...
MmmVomit Posted October 8, 2007 Author Share Posted October 8, 2007 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'. Quote Link to comment https://forums.phpfreaks.com/topic/72322-solved-getting-post-data-from-multiple-select-fields/#findComment-364689 Share on other sites More sharing options...
MmmVomit Posted October 8, 2007 Author Share Posted October 8, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/72322-solved-getting-post-data-from-multiple-select-fields/#findComment-364692 Share on other sites More sharing options...
MmmVomit Posted October 8, 2007 Author Share Posted October 8, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/72322-solved-getting-post-data-from-multiple-select-fields/#findComment-364704 Share on other sites More sharing options...
kenrbnsn Posted October 8, 2007 Share Posted October 8, 2007 Here's the result after selecting two of the options: Array ( [bar] => Array ( [0] => 2 [1] => 4 ) [submit] => Submit ) Ken Quote Link to comment https://forums.phpfreaks.com/topic/72322-solved-getting-post-data-from-multiple-select-fields/#findComment-364710 Share on other sites More sharing options...
MmmVomit Posted October 8, 2007 Author Share Posted October 8, 2007 Great. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/72322-solved-getting-post-data-from-multiple-select-fields/#findComment-364743 Share on other sites More sharing options...
darkhappy Posted April 9, 2008 Share Posted April 9, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/72322-solved-getting-post-data-from-multiple-select-fields/#findComment-512623 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.