在PHP中使用带有隐藏输入字段的标头

时间:2013-03-09 06:51:10

标签: php validation header hidden-field

我正在我的HTML FORM INPUT字段的php文件中创建验证函数。 如果没有正确填写,该函数应跳回到表单,如果填写正确,则跳转到新页面。 如何从表单发送信息而不必使用标题在URL中写入信息?

index.php中的表单:

<form method='post' action='validate.php'>
Name: <input type='text' name='name'>
Job: <input type='text' name='job'>
City: <input type='text' name='city'>
<input type='submit' value='Submit'>
</form>

Validate.php:

if (!empty($_REQUEST['name']) And (!empty($_REQUEST['job'])) And (!empty($_REQUEST['city']))){
$name = $_REQUEST['name'];
$job = $_REQUEST['job'];
$city = $_REQUEST['city'];
header("Location: go.php?name=$name&&job=$job&&city=$city");
}

else {
header("Location: index.php");
}

如果只有一个尚未填写的字段,我还希望跳回index.php以获取已填写的信息。

1 个答案:

答案 0 :(得分:1)

你不应该那样做 不要跳到任何地方。

  • 使index.php成为表单
  • 的操作
  • 在该文件中检测表单是否已发送并包含Validate.php
  • 如果验证失败,只需再次显示您的表单并填写所有字段(不要忘记htmlspecialchars())

HTML标准要求每个表单值(以及textarea内容)使用htmlspecialchars()进行编码