好的,我有1个html页面设置HTML multiple Attribute from。这是它的设置方式:
<!DOCTYPE html>
<html>
<body>
<form action = "otherpage.php" method= "post">
<select name = "cars[]" multiple="multiple" size="4">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<button type = "submit" value= "Submit">Submit</button>
<button type = "reset" value= "Clear">Clear</button>
</form>
</body>
</html>
我有第二个php页面,我们称之为&#34; otherpage.php&#34;这就是它的设置方式:
<!DOCTYPE html>
<html>
<body>
<form action = "" method= "post">
Name: <input type = "text" name = "yourname" />
<button type = "submit" value= "Submit2">Submit</button>
<input type="hidden" name="f" value=<?php $cars= array(); $cars= $_POST['cars']; print_r($cars); ?> />
<?php
if(isset($_POST['f'])) {echo ($_POST['f']);}
?>
</body>
</html>
当我在第一个html页面上点击第一个提交时,阵列打印出来就很好了。然后,当我再次点击另一个提交&#34; otherpage.php&#34;时,该数组不再存在,我收到错误。无论我在otherpage.php上提交了多少次,我怎样才能让汽车阵列永远保留?
答案 0 :(得分:0)
最简单的方法是将数据存储在会话变量中。
您必须在要使用它的每个文件的顶部安装session_start()。
然后当你得到$ _POST值时:
$_SESSION['post'] = $_POST;
之后,您可以在任何其他文件上执行您想要的操作。
答案 1 :(得分:0)
你需要将数组内联到字符串并再次爆炸以获取数组或将其编码为json字符串并重新编码它以任何方式工作
<?php
$cars = isset($_POST['cars']) ?
$_POST['cars'] : isset($_POST['f']) ? explode(',',$_POST['f']) : [];
?>
DOCTYPE html>
<html>
<body>
<form action = "" method= "post">
Name: <input type = "text" name = "yourname" />
<button type = "submit" value= "Submit2">Submit</button>
<input type="hidden" name="f" value="<?php echo implode(',', $cars)?>"/>
</body>
</html>
答案 2 :(得分:0)
我知道你已经接受了答案,但放弃我的测试而不向你展示我的版本也是很小的。
echo '<pre>'...
仅用于显示结果。请注意使用value="<?php echo print_r($cars, true); ?>"
。
<?php
if (isset($_POST['cars'])) {
$cars = $_POST['cars'];
} elseif (isset($_POST['f'])) {
$cars = $_POST['f'];
} else {
$cars = array();
}
echo '<pre>' . print_r($cars, true) . '</pre>';
?>
<!DOCTYPE html>
<html>
<body>
<form action="" method="post">
Name: <input type="text" name="yourname" />
<?php
foreach ($cars as $car) {
echo '<br/>' . $car . '<input type="radio" name="car" value="' . $car . '" />';
}
?>
<button type="submit" value="Submit2">Submit</button>
<input type="hidden" name="f" value="<?php echo print_r($cars, true); ?>" />
</form>
</body>
</html>
祝你好运。
您不再需要隐藏输入。我也更改了页面名称和一些HTML。
<!DOCTYPE html>
<html>
<head>
<title>Cars selecting page</title>
</head>
<body>
<form action="cars_list.php" method= "post">
<select name = "cars[]" multiple="multiple" size="4">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<button type="submit" id="selectSubmit" name="selectSubmit" value="Submit">
Cars selected. List them!
</button>
</form>
</body>
</html>
<?php
session_start();
// Upon clicking on "selectSubmit" button.
if (isset($_POST['selectSubmit'])) {
// Set POST received cars list as session variable.
$_SESSION['cars'] = isset($_POST['cars']) ? $_POST['cars'] : array();
echo 'Called by CARS SELECT page.';
}
// Upon clicking on "listSubmit" button.
if (isset($_POST['listSubmit'])) {
echo 'Called by CARS LIST page.';
}
// Test display of SESSION array.
echo '<br/><br/>Session is: <pre>' . print_r($_SESSION, true) . '</pre>';
// Read cars list from session.
$cars = $_SESSION['cars'];
?>
<!DOCTYPE html>
<html>
<head>
<title>Cars listing page</title>
</head>
<body>
<hr/>
<form action="" method="post">
Name: <input type="text" name="yourname" />
<?php
foreach ($cars as $car) {
echo '<br/>' . $car . '<input type="radio" name="car" value="' . $car . '" />';
}
?>
<button type="submit" id="listSubmit" name="listSubmit" value="Submit">Refresh page!</button>
</form>
</body>
</html>