我在做一个PHP脚本,存储3个数组:$images,$urls,$titles基于PHP文件中表单的输入数据.
我想在页面的第一部分打印这些数组的值,然后使用数组的值预先填充表单的输入字段.此外,当用户修改输入字段并单击"保存"时,页面应该使用修改后的版本重新加载.
我的问题是,在浏览器中每次调用php文件时,变量的值都会被删除.有没有办法存储数组的值,以便表单总是预先填充最后保存的值?
<?php
//save the arrays with the form data
$images = array($_POST["i0"],$_POST["i1"],$_POST["i2"],$_POST["i3"]);
$urls = array($_POST["u0"],$_POST["u1"],$_POST["u2"],$_POST["u3"]);
$titles = array($_POST["t0"],$_POST["t1"],$_POST["t2"],$_POST["t3"]);
//print the arrays
print_r($images);
print_r($urls);
print_r($titles);
//create the form and populate it
echo "<p><form method='post' action='".$_SERVER['PHP_SELF']."';";
$x = 0;
while ($x <= 3) {
echo"<div>
<input name='i".$x."' type='text' value='".$images[$x]."'>
<input name='u".$x."' type='text' value='".$urls[$x]."'>
<input name='t".$x."' type='text' value='".$titles[$x]."'>";
$x++;
}
?>
<br>
<input type="submit" name="sumbit" value="Save"><br>
</form>
Run Code Online (Sandbox Code Playgroud)
Vik*_*ikk 15
将变量存储在PHP会话中.
session_start();
$_SESSION['images'] = $images;
Run Code Online (Sandbox Code Playgroud)
然后在下一个(或任何其他)页面上,您可以检索以下值:
session_start();
$images = $_SESSION['images'];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18871 次 |
| 最近记录: |