提交表单后,我在加载新的“thankyou.php”文件时遇到问题。表单将数据发送给自己,如果所有数据都通过验证,它会继续将它们保存在数据库中。
“thankyou.php”加载很多,但“signup.php”仍然存在,两个文件的输出仍然在同一页面中。我想要它在一个新的页面上,但它不会工作。
这是我的卫生:
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["firstName"]))
{$Err[] = "* First Name is required";}
else
{
$name = test_input($_POST["firstName"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name))
{
$Err[] = "Only letters are allowed in First Name";
}
}
if (empty($_POST["email"]))
{$Err[] = "* Email is required";}
else
{
$email = test_input($_POST["email"]);
// check if e-mail address syntax is valid
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
{
$Err[] = "Invalid email format";
}
}
}
和形式:
<div id = "signupform">
" > 电子邮件地址名字
和数据库连接和插入:
try {
$conn = new PDO('mysql:host=localhost; dbname=mydb', 'root', '');
$conn->setAttribute(PDO:: ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $pe) { echo('连接错误,因为:' .$pe->getMessage()); }
//如果值不为空并经过清理,则将数据插入数据库 if (!empty($_POST["firstName"]) && !empty($_POST["email"])) { $qry = "INSERT INTO userdetails (email, firstName) 值 (?, ?)";
$q = $conn->prepare($qry) or die("ERROR: " . implode(":", $conn->errorInfo()));
$q->bindParam(1, $email);
$q->bindParam(2, $firstName);
try {
if($q->execute()){
header("Location: invoice.php");
exit;
}
}
catch(PDOException $pe) {
echo('Connection error, because: ' .$pe->getMessage());
}
}
仅此而已,但它仍未加载新页面。