Reputation: 334
I have an array named $content_ids which I am trying to post as a hidden field in a form.
I found out how to do this from another answer here but I cant get it to work.
Here are just a few of my inputs including the hidden field for the array
echo "<input type=\"hidden\" value=\"1\" name=\"e\">";
foreach($content_ids as $ids)
{
echo "<input type=\"hidden\" value=\"".$ids."\" name=\"ids[]\">";
}
echo "<input type=\"hidden\" value=\"".$content[$x]['TranslationID']."\" name=\"translationID\">";
Trying to print_r($_POST['ids']) shows nothing
Trying this:
if($_POST['ids'] != ""){
echo "hello";
}
also gives nothing. But the rest of the data is passing through ok.
Anybody any idea why?
Edit to add: Tested to make sure the array actually contains the data at the point of placing it into the hidden field. printing out the array immediately before the hidden field is set and all displays ok.
Edit to add: how the array is made:
$content_ids = array();
for($i = 0; $i < count($content); $i++)
{
$content_ids[] = array_push($content_ids, $content[$i]['ContentID']);
}
the output for the array is:
Array ( [0] => 2222 [1] => 1 [2] => 1111 [3] => 3 )
I actually dont know why index 1 or index 3 are there. They are not part of the data from the database. It should only contain 1111 and 2222.
Upvotes: 0
Views: 333
Reputation: 334
I found the problem. I believe the issue was with the creation of the array.
The only thing I changed was:
$content_ids = array();
for($i = 0; $i < count($content); $i++)
{
$content_ids[] = array_push($content_ids, $content[$i]['ContentID']);
}
And I changed it to this:
$content_ids = array();
for($i = 0; $i < count($content); $i++)
{
array_push($content_ids, $content[$i]['ContentID']);
}
I dont know how but this seems to have fixed both the unwanted indexes in the array and the failure to post as a hidden field.
I am now getting the array successfully as a POST variable. Thank you all for the help
Upvotes: 0
Reputation: 6992
I have run this code and able to get all posted values
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>
<form action ="" method="POST" >
<?php
//supose your values are
$content_ids = array('10','20');
$x = 1;
$content[$x]['TranslationID'] =20;
// your code
echo "<input type=\"hidden\" value=\"1\" name=\"e\">";
foreach($content_ids as $ids)
{
echo "<input type=\"hidden\" value=\"".$ids."\" name=\"ids[]\">";
}
echo "<input type=\"hidden\" value=\"".$content[$x]['TranslationID']."\" name=\"translationID\">";
?>
<input type="submit" />
</form>
</body>
</html>
<?php print_r($_POST); ?>
Here the output
Array ( [e] => 1 [ids] => Array ( [0] => 10 [1] => 20 ) [translationID] => 20 )
Upvotes: 2