467,207 Members | 1,308 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,207 developers. It's quick & easy.

Problem with array...

I have a form with 5 dropdown lists I want to put all selected values to an
array called $data[] so i wrote sometthing like this:

for(i=0;i<5;i++){
echo"<select name=\"\$data[]\">";
echo"<option value=\"1\">1</option>";
echo"<option value=\"1\">2</option>";
echo"<option value=\"1\">3</option>";
echo"</select>";
}

So I think this will put all selected values into an array.
But the problem is that when user submits the form this array goes to $_POST
array
And I have no idea how to read a value from an array $data which is into
array $_POST and use it as a part of "if" statement

for example
if (value_from_array==5){
// instructions
}

Thanks for help
Leszek
Feb 1 '06 #1
  • viewed: 1134
Share:
2 Replies
Leszek wrote:
I have a form with 5 dropdown lists I want to put all selected values to an
array called $data[] so i wrote sometthing like this:

for(i=0;i<5;i++){
echo"<select name=\"\$data[]\">";
echo"<option value=\"1\">1</option>";
echo"<option value=\"1\">2</option>";
echo"<option value=\"1\">3</option>";
echo"</select>";
}

So I think this will put all selected values into an array.
But the problem is that when user submits the form this array goes to $_POST
array
And I have no idea how to read a value from an array $data which is into
array $_POST and use it as a part of "if" statement

$value_from_array=$_POST['$data'];
//for example
if ($value_from_array[0]==5){
// instructions
}

But would be easier you didn't use the $ in the name

for(i=0;i<5;i++){
echo"<select name=\"\data[]\">";
echo"<option value=\"1\">1</option>";
echo"<option value=\"1\">2</option>";
echo"<option value=\"1\">3</option>";
echo"</select>";
}

$value_from_array=$_POST['data'];
if ($value_from_array[0]==5){
// instructions
}

//Aho
Feb 1 '06 #2
I don't exactly understand what you want to do but I hope this will
help ypu getting your values:

<!-- *snip* -->

<html>
<form action="NewFile.php" method="POST">

<?

for($i=0;$i<5;$i++){
echo"<select name=\"test[]\">";
echo"<option value=\"1\">1</option>";
echo"<option value=\"2\">2</option>";
echo"<option value=\"3\">3</option>";
echo"</select>";

}

?>
<INPUT type="submit">
</form>
<?

# this will show you all values that are in $_POST
echo '<pre>', print_r($_POST), '</pre>';

# extract the array that contains all 5 values of the drop downs
$your_data = $_POST["test"];

# display the array
echo '<pre>', print_r($your_data), '</pre>';

# run through the array
foreach ($your_data as $value_from_array)
{
# ...and do with the values whatever you want to do
if ($value_from_array==5)
{
// instructions
# :-)
}
}

?>

</html>

<!-- *snap* -->


Leszek schrieb:
I have a form with 5 dropdown lists I want to put all selected values to an
array called $data[] so i wrote sometthing like this:

for(i=0;i<5;i++){
echo"<select name=\"\$data[]\">";
echo"<option value=\"1\">1</option>";
echo"<option value=\"1\">2</option>";
echo"<option value=\"1\">3</option>";
echo"</select>";
}

So I think this will put all selected values into an array.
But the problem is that when user submits the form this array goes to $_POST
array
And I have no idea how to read a value from an array $data which is into
array $_POST and use it as a part of "if" statement

for example
if (value_from_array==5){
// instructions
}

Thanks for help
Leszek


Feb 1 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by JesusFreak | last post: by
8 posts views Thread by intrepid_dw@hotmail.com | last post: by
4 posts views Thread by daroman@gmx.net | last post: by
9 posts views Thread by weidongtom@gmail.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.