Jump to content

retrieve value from 3 dropdown menus which are the same name?


zac1987

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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.