Reputation: 500
I am sending a multidimensional array by POST using the following:
<form action="fixtures.php" method="POST">
<input type="hidden" name="day" value="<?= $day ?>">
<input type="hidden" name="month" value="<?= $month ?>">
<input type="hidden" name="year" value="<?= $year ?>">
<input type="hidden" name="league" value="<?= $league ?>">
<input type="hidden" name="addFixtures" value="1">
<?php
for($i = 0; $i < count($fixtures); $i++){
$team_a_name = $fixtures[$i]['team-a-name'];
$team_b_name = $fixtures[$i]['team-b-name'];
$fixtDate = $fixtures[$i]['Date'];
echo "<input type='hidden' name=\"fixtures[$i]['team-a-name']\" value='$team_a_name'>";
echo "<input type='hidden' name=\"fixtures[$i]['team-b-name']\" value='$team_b_name'>";
echo "<input type='hidden' name=\"fixtures[$i]['date']\" value='$fixtDate'>";
}
?>
<input type="submit" value="Add Fixtures">
</form>
However, when I try to access the values stored inside $_POST['fixtures']
, using the following:
if(isset($_POST['addFixtures'])){
print_r($_POST['fixtures'][0]);
echo "<br><br>";
print($_POST['fixtures'][0]['date']);
exit;
}
I get this output:
Array ( ['team-a-name'] => TEAM A ['team-b-name'] => TEAM B ['date'] => 5-4-2016 )
Notice: Undefined index: date in /Applications/XAMPP/xamppfiles/htdocs/soccerdome/fixtures.php on line 33
The same notice appears if I try to access 'team-a-name' or 'team-b-name'.
Why can't I access these values, when print_r
on $_POST['fixtures'][0]
clearly shows that they are in the array!
EDIT
When printing out $_POST[]
, I get:
Array
(
[day] => 5
[month] => 4
[year] => 2016
[league] => prem
[addFixtures] => 1
[fixtures] => Array
(
[0] => Array
(
['team-a-name'] => TEAM A
['team-b-name'] => TEAM B
['date'] => 5-4-2016
)
[1] => Array
(
['team-a-name'] => TEAM C
['team-b-name'] => TEAM D
['date'] => 5-4-2016
)
[2] => Array
(
['team-a-name'] => TEAM A
['team-b-name'] => TEAM D
['date'] => 12-4-2016
)
[3] => Array
(
['team-a-name'] => TEAM B
['team-b-name'] => TEAM C
['date'] => 12-4-2016
)
[4] => Array
(
['team-a-name'] => TEAM A
['team-b-name'] => TEAM C
['date'] => 19-4-2016
)
[5] => Array
(
['team-a-name'] => TEAM D
['team-b-name'] => TEAM B
['date'] => 19-4-2016
)
)
)
EDIT 2
For some reason it is now working! I have no idea how, just be refreshing the page multiple times, the error message disappeared and the value inside POST was printed!
Upvotes: 0
Views: 2303
Reputation: 307
print_r the whole post array.i.e.print_r($_POST).Then you will have the exact keys and values assigned and will be successful in displaying the array.
Upvotes: 0
Reputation: 76
Easiest way to traverse the post data is using foreach
. You can do this as following:
if(isset($_POST['addFixtures']))
{
foreach($_POST['fixtures'] as $key=>$value)
{
echo "<br><br>";
print($value['date']);
}
}
exit;
Upvotes: 1
Reputation: 167182
Since you don't have any special characters in date
, it is better to give without a '
:
echo "<input type='hidden' name=\"fixtures[$i]['team-a-name']\" value='$team_a_name'>";
echo "<input type='hidden' name=\"fixtures[$i]['team-b-name']\" value='$team_b_name'>";
echo "<input type='hidden' name=\"fixtures[$i][date]\" value='$fixtDate'>";
Or change it to:
echo "<input type='hidden' name=\"fixtures[$i][team-a-name]\" value='$team_a_name'>";
echo "<input type='hidden' name=\"fixtures[$i][team-b-name]\" value='$team_b_name'>";
echo "<input type='hidden' name=\"fixtures[$i][date]\" value='$fixtDate'>";
Always use var_dump($_POST)
or print_r($_POST)
to check what's happening in reality. :)
Upvotes: 0