【问题标题】:redirect to new page after form submitted and validated using php表单提交并使用 php 验证后重定向到新页面
【发布时间】:2014-10-04 03:56:52
【问题描述】:

我一直在尝试使用 php 验证来改进我们的联系表单,我可以让表单得到验证并在每个字段未正确填写时显示错误,当表单完成并正确验证时会显示成功消息,以及包含表单信息的电子邮件已发送给我。

我的问题是我无法让代码的 header('location'...) 部分工作。提交后,表单下方不仅会显示“表单已成功提交”,而是希望它转到“谢谢”页面。

这是我的代码:

这是我的表单页面:

<?php 
include('validate.php'); 
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>TEST</title>
<style>    
    input, textarea {font-size: 1em;}
    p.error {background: #ffd; color: red;}
    p.error:before {content: "Error:";}
    p.success {background: #ffd; color: green;}
    p.success:before {content: "Success:";}
    p.error, p.success {font-weight: bold;}
</style>
</head>
<body>
<h2>Please fill up the form below and submit.</h2>
<?=$error?>
<form action="html_form_to_validate.php" method="post">
<table>
<tr>
   <td>Name: </td>
<td>
<input type="text" name="name" placeholder="Name *(Required)" value="<?=@$name?>"/>    </td>
</tr>
<tr>
    <td>Company: </td>
<td><input type="text" name="company" placeholder="Company" value="<?=@$company?>"/>    </td>
</tr>
<tr>
<td>Hear: </td>
<td><input type="text" name="hear"  placeholder="How did you hear about us?" value="<?=@$hear?>"/></td>
</tr>
<tr>
<td>Email: </td>
<td><input type="text" name="email" value="<?=@$email?>"/></td>
</tr>
<tr>
<td>Phone: </td>
<td><input type="text" name="phone" value="<?=@$phone?>"/></td>
</tr>
<tr>
<td>Message: </td>
<td><textarea name="comments"><?=@$comments?></textarea></td>
</tr>
</table>            
<input type="submit" name="submit" value="Submit"/> <input type="reset" name="reset" value="Reset"/>
</form>

<?php
if (isset($_POST['submit']) && $error == '') { // if there is no error, then process further
echo "<p class='success'>Form has been submitted successfully.</p>"; // showing success message

$name=$_POST['name']; 
$company=$_POST['company']; 
$telephone=$_POST['telephone']; 
$email=$_POST['email']; 
$hear=$_POST['hear']; 
$comments=$_POST['comments']; 

$to="chris@example.com";
$subject="Request for information";
$from="sales@example.com";
$message="<html>
<body topmargin='0'>

<div align='center'>
<table border='0' width='736' id='table1' cellspacing='0' cellpadding='0'>
<tr>
<td height='129' bgcolor='#EDEDE9' width='736' valign='top' style='margin-left: 10'>
<table border='0' id='table2' cellspacing='10' cellpadding='15' width='726'>
<tr>
<td width='676' valign='top' bgcolor='#FFFFFF'>
<p align='left'>
<img border='0' src='http://www.example.com/images/logo.png' align='left' hspace='0'>    </p>
<p align='left'>
<br><br>
<b>
<font face='Verdana' color='#0078c1' style='font-size: 20pt'>
&nbsp;&nbsp;Request for information</font></b></p>
<p align='left'>&nbsp;</p>
</td>
</tr>
<tr>
<td width='676' valign='top' bgcolor='#FFFFFF'>
<p>
<font face='Verdana' size='2'>The following person has been on <a href='http://www.example.com'>
<font color='#0078c1'>www.example.com</font></a> and requesting information from our 'contact form'.</font></p>
<p>
<font face='Verdana' size='2'>Name: </font><font face='Verdana' size='2'><b>$name</b>    </font></p>
<p>
<font face='Verdana' size='2'>Company: </font><font face='Verdana' size='2'><b>$company</b></font></p>
<p>
<font face='Verdana' size='2'>Telephone: <font face='Verdana' size='2'><b>$telephone</b></font></p>
<p>
<font face='Verdana' size='2'>Email: <font face='Verdana' size='2'><b>$email</b></font></p>
<p>
<font face='Verdana' size='2'>Heard about us from: </font><font face='Verdana' size='2'><b>$hear</b></font></p>
<p>
<font face='Verdana' size='2'>Message: <font face='Verdana' size='2'><b>$comments</b></font></p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</body>
</html>

";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers  .= "From: $from\r\n"; 
@mail($to, $subject, $message, $headers);
header('Location: http://www.example.com/contact/thank-you.php'); 
}
?> 
</body>
</html>

还有我的 validate.php 文件:

<?php

$error = ""; // Initialize error as blank

if (isset($_POST['submit'])) { // check if the form is submitted
#### removing extra white spaces & escaping harmful characters ####
$name             = trim($_POST['name']);
$company         = trim($_POST['company']);
$hear             = trim($_POST['hear']);
$email                 = $_POST['email'];
$phone                = $_POST['phone'];
$comments                = $_POST['comments'];

#### start validating input data ####
#####################################

# Validate Name #

    // if name is not 3-20 characters long, throw error
    if (strlen($name) < 3 OR strlen($name) > 20) {
        $error .= '<p class="error">Name should be within 3-20 characters long.</p>';
    }

# Validate Email #
    // if email is invalid, throw error
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { // you can also use regex to do same
        $error .= '<p class="error">Enter a valid email address.</p>';
    }

# Validate Phone #
    // if phone is invalid, throw error
    if (!ctype_digit($phone) OR strlen($phone) < 9) {
        $error .= '<p class="error">Enter a valid telephone number.</p>';
    }

# Validate Comments #
    if (strlen($comments)==0 OR strlen($comments)>240) {
        $error .= '<p class="error">Please enter your message less than 240 characters.</p>';
    }

#### end validating input data ####
#####################################
}

我是一个新手,并且已经使用其他地方的脚本来做我想做的事情 - 所以请友好,哈哈

谢谢

克里斯

【问题讨论】:

标签: php forms validation


【解决方案1】:

验证和重定向需要在任何输出到浏览器之前发生,因此您需要在显示表单之前放置​​header 函数。

或者如果你必须在表单之后进行重定向,你可以使用 javascript 重定向而不是 PHP。

<script>
    // just redirect
    window.location='http://your-site.dev/new-url.php'
    // or redirect after showing form
    setTimeout(function () {
        window.location.href = 'http://your-site.dev/new-url.php'; 
    }, 2000); //will call the function after 2 secs.
</script>

【讨论】:

    【解决方案2】:

    我个人喜欢这种方法...

    echo '<meta http-equiv="refresh" content="0;URL=www.mylink.com" />';
    

    【讨论】:

    【解决方案3】:

    在我的 PHP 代码中,我总是使用 header("refresh:0;url=the_url_here"); 而不是 header('Location: url');

    试试吧,也许对你有用。

    【讨论】:

    • 谢谢 - 对不起,我在下面看到你的回答 - 很有魅力
    猜你喜欢
    • 1970-01-01
    • 2014-03-10
    • 1970-01-01
    • 2020-05-27
    • 2013-06-14
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    相关资源
    最近更新 更多