我在这里做错了什么?
firstpage.php
<html>
<body>
<?php
if (!isset($_GET['error']))
$error=array();
else
$error = $_GET['error'];
?>
<script>
function clearText(field){
if (field.defaultValue == field.value) field.value = '';
else if (field.value == '') field.value = field.defaultValue;
}
</script>
<?php
foreach($error as $key => $value){
if ($value != '')
echo '<p style="color:red;">', $value, '</p>';
}
?>
<form method="GET" action="validar.php">
First Name: <input type="text" name="firstname" value="first name here" onFocus="clearText(this)" onBlur="clearText(this)"> <br>
Last Name: <input type="text" name="lastname" value="last name here" onFocus="clearText(this)" onBlur="clearText(this)"> <br><br>
<input type="submit" value="SEND">
</form>
</body>
</html>
secondPage.php
<?php
# confirmation.php
$firstname = $_GET['firstname'];
$latname = $_GET['lastname'];
?>
<html>
<body>
<h3>Congratulations <?php echo $firstname; $lastname; ?>, you have been successfully registered</h3>
</body>
</html>
validar.php
<?php
$firstname = $_GET['firstname'];
$lastname = $_GET['lastname'];
$error['firstname'] = '';
$error['lastname'] = '';
$firstPage = "firstPage.php";
$secondPage = "secondPage.php";
$server_dir = $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['PHP_SELF']), '/\\') . '/';
if($firstname == "first name here"){
$firstname == "";
$error['firstname'] = 'Please introduce firstname <br>';
}
if($lastname == "last name here"){
$lastname == "";
$error['lastname'] = 'Please introduce last <br>';
}
if($error == ''){
$query_string = '?firstname=' . $firstname;
header('Location: http://' . $server_dir . $secondPage . $query_string);
}
else {
$query_string = http_build_query($error);
header('Location: http://' . $server_dir . $firstPage . $query_string);
}
?>
错误消息:拒绝访问!我是PHP的新手,所以我不知道如何构建一个包含多个字段错误的查询字符串。我正在使用带有键作为“字段值”的数组,因为我认为它更容易。但我不是这是最好的方法,即使是这样,我也不确定我是不是做得好。任何帮助,将不胜感激, 约翰
答案 0 :(得分:0)
http_build_query函数仅构建查询。您需要在问号前加上标记查询字符串的开头。
让我们拿你的代码:
header('Location: http://' . $server_dir . $firstPage . $query_string);
您需要插入“?”在firstPage和query_string之间:
header('Location: http://' . $server_dir . $firstPage . '?' . $query_string);