如何在多个页面加载中存储变量值 [英] how to store variable values over multiple page loads

查看:88
本文介绍了如何在多个页面加载中存储变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个php脚本,该脚本基于php文件中表单的输入数据来存储3个数组:$images$urls$titles.

I'm making a php script that stores 3 arrays: $images, $urls, $titles based on the input data of the form within the php file.

我想在页面的第一部分中打印这些数组的值,然后用数组的值预填充表单的输入字段.同样,当用户修改输入字段并单击保存"时,页面应重新加载修改后的版本.

I want to print the values of these arrays in the first part of the page and then to pre-fill the form's input fields with the value of the arrays. Also when a user modifies an input field and clicks on "Save" the page should reload with the modfied version.

我的问题是在浏览器中每次调用php文件时,变量的值都会被删除.有没有一种方法可以存储数组的值,以便始终用最后保存的值来预先填充表单?

My problem is that on each call of the php file in a browser the value of the variables gets deleted. Is there a way to store the values of the array so that the form always gets pre-filled with the last saved values?

<?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>

推荐答案

将变量存储在PHP会话中.

Store the variables in a PHP session.

session_start();
$_SESSION['images'] = $images;

然后在下一页(或其他任何页面)上,您可以按以下方式检索值:

Then on next (or any other) page, you can retrieve the values as:

session_start();
$images = $_SESSION['images'];

这篇关于如何在多个页面加载中存储变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆