我有一个名为 $content_ids 的数组,我试图将其作为表单中的隐藏字段发布。
我从这里的另一个答案中发现了如何做到这一点,但我无法让它工作。
这里只是我的一些输入,包括数组的隐藏字段
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">";
尝试print_r($_POST['ids'])什么也没显示
尝试这个:
if($_POST['ids'] != ""){
echo "hello";
}
也什么也没给。但是其余的数据正常通过。
有人知道为什么吗?
编辑以添加:经过测试以确保数组在将其放入隐藏字段时实际包含数据。 在设置隐藏字段之前立即打印出数组,并且所有显示正常。
编辑以添加:数组是如何制作的:
$content_ids = array();
for($i = 0; $i < count($content); $i++)
{
$content_ids[] = array_push($content_ids, $content[$i]['ContentID']);
}
数组的输出为:
数组 ( [0] => 2222 [1] => 1 [2] =>1111 [3] => 3 )
我实际上不知道为什么索引 1 或索引 3 在那里。它们不是数据库中数据的一部分。它应该只包含 1111 和 2222。
我已经运行了这段代码,并且能够获取所有发布的值
<!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); ?>
这里的输出
Array ( [e] => 1 [ids] => Array ( [0] => 10 [1] => 20 ) [translationID] => 20 )
我发现了问题。我相信问题出在数组的创建上。
我唯一改变的是:
$content_ids = array();
for($i = 0; $i < count($content); $i++)
{
$content_ids[] = array_push($content_ids, $content[$i]['ContentID']);
}
我把它改成这样:
$content_ids = array();
for($i = 0; $i < count($content); $i++)
{
array_push($content_ids, $content[$i]['ContentID']);
}
我不知道如何,但这似乎已经修复了数组中不需要的索引以及无法作为隐藏字段发布的问题。
我现在成功地将数组作为 POST 变量获取。谢谢大家的帮助