我需要一些帮助来尝试序列化html中的隐藏元素,而不是将值传递给带有post的php。这是我的代码:
<form action = "index.php" method = "POST" >
Enter name: <input type ="text" name = "name"><br></br>
enter ID: <input type = "text" name = "id"><br></br>
<input type = "hidden"
id = "hiddenStrArr"
name "hiddenStrArr"
value = "<?php print base64_encode(serialize("$arrayVar"))?>">
<input type = "submit" name = "submitButton" value = "Submit">
</form>
<?php
$nameExists = FALSE;
$name = ($_POST['name']);
$id = ($_POST['id']);
$hiddenStrArr = $_POST["hiddenStrArr"];
$arrayVar = unserialize(base64_decode($hiddenArray));
我在我的php中有一个foreach循环中的数组,它将检查来自用户输入的数组中的数据。问题是我的阵列保持不变。如果我再次发布,它将重新开始使用旧数组,但我希望它能够通过用户输入进行更新。我正在尝试按照本指南Passing Array Variables using POST or GET with PHP进行操作,但我并不理解。有人请帮助我或解释这是如何工作的。我收到一个错误,说$ name $ id $ hiddenStrArr和$ arrayVar是索引未定义的。谢谢。
以下是帮助解释正在发生的事情的更多代码:
<?php
$myTwoDimArr = array(1 => array("name1", 0, "123"),
2 => array("name2", 123, "456"),
3 => array("name3", 456, "789"),
4 => array("name3", 789, "101112"));
?>
<form action = "index.php" method = "POST" >
Enter name: <input type ="text" name = "name"><br></br>
enter ID: <input type = "text" name = "id"><br></br>
<input type = "hidden"
id = "hiddenStrArr"
name "hiddenStrArr"
value = "<?php print base64_encode(serialize("$arrayVar"))?>">
<input type = "submit" name = "submitButton" value = "Submit">
</form>
<?php
$nameExists = FALSE;
$name = ($_POST['name']);
$id = ($_POST['id']);
$hiddenStrArr = $_POST["hiddenStrArr"];
$arrayVar = unserialize(base64_decode($hiddenArray));
//this will check values from the html form against
//the values stored in the array than add 5 to $value[1]
foreach($myTwoDimArr as $key => $value){
if($key == $id && $value[0] == $name){
$myTwoDimArr[$key][1] = $myTwoDimArr[$key][1] + 5;
$nameExists = TRUE;
}
} //if values don't exist this creates a new element
//in the declared array
if ($nameExists === FALSE){
$newData = array($name, 1, date("Y-m-d"));
$myTwoDimArr[$id] = $newData;
}
//this will print values from the array
//the values are not being saved, I was hoping to use
//hidden element to accomplish this so that user may input
//more new values or add to existing values
foreach($myTwoDimArr as $key => $value){
echo "<tr><td>".$key."</td>
<td>".htmlspecialchars($value[0])."</td>
<td>".htmlspecialchars($value[1])."</td>
<td>".htmlspecialchars($value[2])."</td></tr>";
}
?>
答案 0 :(得分:0)
您的变量定义在下面您呈现表单。这就是为什么你会得到未定义的错误。那时变量不存在。
在这种情况下你应该做什么,几乎在任何情况下我建议你最后输出你的html。因此在渲染之前进行计算。
<?php
$nameExists = FALSE;
$name = isset($_POST['name']) ? $_POST['name'] : '';
$id = isset($_POST['id']) ? $_POST['id'] : '';
$hiddenStrArr = isset($_POST['hiddenStrArr']) ? $_POST['hiddenStrArr'] : '';
$arrayVar = unserialize(base64_decode($hiddenStrArr));
?>
<form action = "index.php" method = "POST" >
Enter name: <input type ="text" name = "name"><br></br>
enter ID: <input type = "text" name = "id"><br></br>
<input type = "hidden"
id = "hiddenStrArr"
name "hiddenStrArr"
value = "<?php print base64_encode(serialize("$arrayVar"))?>">
<input type = "submit" name = "submitButton" value = "Submit">
</form>