我有一个全局数组变量,用于存储$_POST
变量中的一些值。因为我在if
语句中修改它们,所以我使用了global
这个词。我已经将数组填充为array(0,1)
。鉴于在我的if
语句中修改了值,我仍然得到了我开始时的初始化值。
修改
这是我的formHandler.php
代码:
// get form ID
$formID = $_GET['id'];
// create a global array
$results = array();
// check to see what ID has be retrieved
if ($formID == 1) {
$value = $_POST['emotion'];
$results[0] = $value;
// 302 redirect
header("location: page02.php?results1=$results[0]");
}
else if ($formID == 2) {
$value = $_POST['emotion02'];
$results[1] = $value;
// 302 redirect
header("location: page03.php?&results2=$results[1]");
}
这是我的page01.php
代码:
<h1>This is Page 01</h1>
<form id="form" action="formHandler.php?id=1" method="POST">
<p><input type="radio" value="happy" name="emotion">Happy
<input type="radio" value="excited" name="emotion">Excited
<input type="radio" value="angry" name="emotion">Angry
<input type="radio" value="frustrated" name="emotion">Frustrated
<input type="radio" value="miserable" name="emotion">Miserable
<input type="radio" value="sad" name="emotion">Sad
<input type="radio" value="tired" name="emotion">Tired
<input type="radio" value="calm" name="emotion">Angry</p>
<p><input type="submit" name="submit" value="Submit"></p>
这是我的page02.php
代码:
<h1>This is Page 02</h1>
<form id="form" action="formHandler.php?id=2" method="POST">
<p><input type="radio" value="happy" name="emotion02">Happy
<input type="radio" value="excited" name="emotion02">Excited
<input type="radio" value="angry" name="emotion02">Angry
<input type="radio" value="frustrated" name="emotion02">Frustrated
<input type="radio" value="miserable" name="emotion02">Miserable
<input type="radio" value="sad" name="emotion02">Sad
<input type="radio" value="tired" name="emotion02">Tired
<input type="radio" value="calm" name="emotion02">Angry</p>
<p><input type="submit" name="submit" value="Submit"></p>
这是我的page03.php
代码:
<?php include("formHandler.php"); ?>
<html>
<head>
<title>Audio Survey</title>
</head>
<body>
<h1>Thanks for your submission</h1>
<?php
$results = array();
$results[0] = $_GET['results1'];
$results[1] = $_GET['results2'];
echo "<p>" . $results[0] . " " . $results[1] . "</p>";
?>
</body>
</html>
答案 0 :(得分:1)
答案 1 :(得分:1)
您正在使用$ _GET获取表单ID,但您说它是$ _POST(method =&#34; POST&#34;)表单,因此ID永远不会与您的条件相匹配如果声明。这应该解决它:
$formID = $_POST['id'];
此外,id应该是表单上的输入,以便将其作为$ _POST传递,表单的实际id属性不会自行传递给服务器。
如果你真的通过&#34;?id = x&#34;在action属性的url上,请发布HTML表单。
另外一件事,你不需要,也不应该使用&#34; global&#34;,特别是如果这些变量位于您从中调用它们的同一个文件中
答案 2 :(得分:0)
这里不需要全局变量,所有代码都在同一范围内。