zac1987 Posted March 22, 2011 Share Posted March 22, 2011 I have 3 dropdown menus which have the same name <select name="dropdown">, how to get the value from 3 of them by using php for loop and array? I did try to put array on them before, but seem it can't work. My codes : <?php for ( $counter = 1; $counter <= 3; $counter ++) { if (isset($_POST['submit'])) { //Form has been submitted. $dropvalue = $_POST['dropdown[$counter]']; echo $dropvalue; } } ?> <form action="test8.php" name="frmtest8" method='post'> <select name="dropdown[1]"> <option value="1st">first</option> <option value="2nd">second</option> <option value="3rd">third</option> </select> <select name="dropdown[2]"> <option value="1st">first</option> <option value="2nd">second</option> <option value="3rd">third</option> </select> <select name="dropdown[3]"> <option value="1st">first</option> <option value="2nd">second</option> <option value="3rd">third</option> </select> <input type="submit" name="submit" value="Send" /> </form> It gives error message Parse error: parse error in C:\wamp\www\plekz\test8.php on line 23 I guess the error is because of $_POST['dropdown[$counter]']; I cannot put the two symbols [] inside $_POST[' '] right? How to call the array if cannot put the two symbols? Link to comment https://forums.phpfreaks.com/topic/231338-retrieve-value-from-3-dropdown-menus-which-are-the-same-name/ Share on other sites More sharing options...
PFMaBiSmAd Posted March 22, 2011 Share Posted March 22, 2011 The $_POST variables will be - $_POST['dropdown][1], $_POST['dropdown][2], and $_POST['dropdown][3] You would typically use a foreach() loop - foreach($_POST['dropdown] as $key => $value){ // $key will be 1,2, or 3 // $value will be the submitted value } Link to comment https://forums.phpfreaks.com/topic/231338-retrieve-value-from-3-dropdown-menus-which-are-the-same-name/#findComment-1190646 Share on other sites More sharing options...
zac1987 Posted March 22, 2011 Author Share Posted March 22, 2011 @PFMaBiSmAd , your code works like charm. Thank you. Link to comment https://forums.phpfreaks.com/topic/231338-retrieve-value-from-3-dropdown-menus-which-are-the-same-name/#findComment-1190659 Share on other sites More sharing options...
zac1987 Posted March 22, 2011 Author Share Posted March 22, 2011 May I ask another question? Note that 3 of my dropdown menu has array on its name, eg : <select name="dropdown[1]"> $query2 = "SELECT * FROM messages"; $result2 = mysql_query($query2,$connection) or die (mysql_error()); confirm_query($result2); while($userinfo = mysql_fetch_array($result2)){ echo "<select name=\"dropdown[]\">"; } If I have 10 rows of records on the database, I want the php auto creating 10 dropdown menus with the name=dropdown[1] to dropdown[10], how to do that? Is it I need to use for loop $i = 1, $i <=10, $i++ then put name=dropdown[$i] ? But if I put the for loop inside the while loop, it will create 30 dropdown menus, seriously I don't know how to do that... Or can I do something like this? $count=0; while($userinfo = mysql_fetch_array($result2)){ $count=$count + 1; echo "<select name=\"dropdown[$count]\">"; } Link to comment https://forums.phpfreaks.com/topic/231338-retrieve-value-from-3-dropdown-menus-which-are-the-same-name/#findComment-1190669 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.