Reputation: 945
<?php
$sessionTotal = 10;
for($initial = 1; $initial <= $sessionTotal ; $initial++){
echo '<input type="text" name="menuItems" size="20" /><br /><br/>';
}
//I have a if statement here checking if the submit button isset, yada yada, after I press the submit button, it returns this error -> Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\yada\yada-yada.php on line 43
foreach($_POST['menuItems'] as $value)
{
echo $value;
}
?>
It will echo $value after it is submitted. I have a if statement checking if the submit button isset, yada yada, after I press the submit button, it returns this error -> Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\yada\yada-yada.php on line 43
Upvotes: 2
Views: 395
Reputation: 13557
There is nothing wrong with your foreach
. There is something wrong with your understanding of how PHP parses input-attributes (_POST, _GET).
<input type="text" name="foobar" value="one">
<input type="text" name="foobar" value="two">
<input type="text" name="foobar" value="three">
translates to the application/x-www-form-urlencoded representation foobar=one&foobar=two&foobar=three
.
PHP parses this string into a map (associative array). It does this somewhat like the following code:
<?php
$_GET = array();
$string = 'foobar=one&foobar=two&foobar=three';
$parts = explode('&', $string);
foreach ($parts as $part) {
$p = explode('=', $part);
$_GET[urldecode($p[0])] = urldecode($p[1]);
}
So basically it is assigning $_GET['foobar']
three times, leaving $_GET['foobar'] === 'three'
.
Translated, this is what is happening here:
$_GET['foobar'] = 'one';
$_GET['foobar'] = 'two';
$_GET['foobar'] = 'three';
At this point I'd like to note that other languages (Ruby, Java, …) deal with this quite differently. Ruby for example recognizes the repeating key and builds something similar to $_GET['foobar'] = array('one', 'two', 'three')
.
There is a simple "trick" to tell PHP that the repeating value should be parsed into an array:
<input type="text" name="foobar[]" value="one">
<input type="text" name="foobar[]" value="two">
<input type="text" name="foobar[]" value="three">
will lead to $_GET['foobar'] = array('one', 'two', 'three')
;
Translated, this is what is happening here:
$_GET['foobar'][] = 'one';
$_GET['foobar'][] = 'two';
$_GET['foobar'][] = 'three';
(Note: $array[] = 'value'
is the same as array_push($array, 'value')
)
So whenever you're dealing with repeating key names (or <select multiple>
) you want to add []
to the name, so PHP builds an array from it.
You may also want to know that you can actually specify the array-keys:
<input type="text" name="foobar[hello][world]" value="one">
will lead to $_GET['foobar']['hello']['world'] == 'one'
.
Upvotes: 0
Reputation: 21838
$_POST['menuItems']
is not an array, foreach
only accepts arrays and certain objects.
If you make it
<?php
$sessionTotal = 10;
for($initial = 1; $initial <= $sessionTotal ; $initial++){
echo '<input type="text" name="menuItems[]" size="20" /><br /><br/>';
}
//I have a if statement here checking if the submit button isset, yada yada, after I press the submit button, it returns this error -> Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\yada\yada-yada.php on line 43
if ( is_array( $_POST['menuItems'] ) )
foreach($_POST['menuItems'] as $value)
{
echo $value;
}
?>
It should work.
Upvotes: 3