在表单提交并使用php进行验证后,重定向到新页面 [英] redirect to new page after form submitted and validated using php

查看:92
本文介绍了在表单提交并使用php进行验证后,重定向到新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力通过php验证来改进我们的联系表单,当每个字段未被正确填写时,我可以获取要验证的表单并显示错误,当表单完成和验证正确时显示成功消息,电子邮件是通过表单中的信息发给我的。



我的问题是我无法获取代码的头部('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>请填写下面的表单并提交。< / h2>
<?= $ error?>
< form action =html_form_to_validate.phpmethod =post>
< table>
< tr>
< td>名称:< / td>
< td>
< input type =textname =nameplaceholder =Name *(Required)value =<?= @ $ name?>/> < / TD>
< / tr>
< tr>
< td>公司:< / td>
< td>< input type =textname =companyplaceholder =Companyvalue =<?= @ $ company?>/> < / TD>
< / tr>
< tr>
< td>听到:< / td>
< td>< input type =textname =hearplaceholder =您是如何知道我们的?值= LT; = @ $听到>?/>< / TD>
< / tr>
< tr>
< td>电子邮件地址:< / td>
< td>< input type =textname =emailvalue =<?= @ $ email?>/>< / td>
< / tr>
< tr>
< td>电话:< / td>
< td>< input type =textname =phonevalue =<?= @ $ phone?>/>< / td>
< / tr>
< tr>
< td>讯息:< / td>
< td>< textarea name =comments><?= @ $ comments?>< / textarea>< / td>
< / tr>
< / table>
< input type =submitname =submitvalue =Submit/> < input type =resetname =resetvalue =Reset/>
< / form>

<?php
if(isset($ _ POST ['submit'])&& $ error ==''){//如果没有错误,进一步
回声< p class ='success'>表单已成功提交。< / p>; //显示成功消息

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

$ to =chris@example.com;
$ subject =索取资料;
$ from =sales@example.com;
$ message =< html>
< body topmargin ='0'>

< div align ='center'>
<
< tr>
< td height ='129'bgcolor ='0' '#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;请求信息< / font>< / b>< / p>
< p align ='left'>& nbsp;< / p>
< / td>
< / tr>
< tr>
< td width ='676'valign ='top'bgcolor ='#FFFFFF'>
< p>
< font face ='Verdana'size ='2'>以下人已在< a href ='http://www.example.com'>
< font color ='#0078c1'> www.example.com< / font>< / a>并从我们的联系表单中请求信息。< / font>< / p>
< p>
< font face ='Verdana'size ='2'>名称:< / font>< font face ='Verdana'size ='2'>< b> $ name< / b> ; < /字体>< / p为H.
< p>
< font face ='Verdana'size ='2'>公司:< / font>< font face ='Verdana'size ='2'>< b> $ company< / b> ;< /字体>< / p为H.
< p>
< font face ='Verdana'size ='2'>电话:< font face ='Verdana'size ='2'>< b> $ telephone< / b>< / font> ;< / p为H.
< p>
< font face ='Verdana'size ='2'>电子邮件:< font face ='Verdana'size ='2'>< b> $ email< / b>< / font> ;< / p为H.
< p>
< font face ='Verdana'size ='2'>听到关于我们的信息:< / font>< font face ='Verdana'size ='2'>< b> $ hear< ; / b>< /字体>< / p为H.
< p>
< font face ='Verdana'size ='2'>留言:< font face ='Verdana'size ='2'>< b> $ comments< / b>< / font> ;< / p为H.
< / td>
< / tr>
< / table>
< / td>
< / tr>
< / table>
< / div>
< / body>
< / html>
$ b $;
$ headers =MIME-Version:1.0 \r\\\
;
$ headers。=Content-type:text / html; charset = iso-8859-1 \r\\\
;
$ headers。=From:$ from\r\\\
;
@mail($ to,$ subject,$ message,$ headers);
header('Location:http://www.example.com/contact/thank-you.php');
}
?>
< / body>
< / html>

我的validate.php文件:

 <?php 

$ error =; //将错误初始化为空白

if(isset($ _ POST ['submit'])){//检查表单是否已提交
####删除多余的空格和转义有害字符####
$ name = trim($ _ POST ['name']);
$ company = trim($ _ POST ['company']);
$ hear = trim($ _ POST ['hear ']);
$ email = $ _POST ['email'];
$ phone = $ _POST ['phone'];
$ comments = $ _POST ['comments'];

####开始验证输入数据####
########################### ##########

#验证名称#

//如果名称长度不是3-20个字符,则抛出错误
if(strlen($ name)< 3或strlen($ name)> 20){
$ error。='< p class =error>名称的长度应在3-20个字符之内。< / p>';

$ b $#验证电子邮件#
//如果电子邮件无效,则抛出错误
if(!filter_var($ email,FILTER_VALIDATE_EMAIL)){// you can也使用正则表达式来做同样的
$错误。='< p class =error>输入一个有效的电子邮件地址。< / p>';
}

#验证电话#
//如果电话无效,则抛出错误
if(!ctype_digit($ phone)or strlen($ phone)< 9){
$ error。='< p class =error>输入一个有效的电话号码。< / p>';
}

#验证注释#
if(strlen($ comments)== 0或strlen($ comments)> 240){
$ error。= '< p class =error>请输入您的信息少于240个字符。< / p>';
}

#### end验证输入数据####
##################### ################
}

我是新手,并使用其他地方的脚本来做我想做的事情 - 所以请大声笑



谢谢
$ b $在我的PHP代码中,我总是使用头(refresh:0;)。 url = the_url_here); 代替 header('Location:url');



试试吧,也许它适用于你。


I have been trying to improve our contact forms with php validation, I can get the form to be validated and show errors when each field is not filled in correctly, success message appears when the form is completed and validated correctly, and an email is sent to me with the information from the form.

My problem is that I can not get the header('location'...) part of the code to work. Rather than just having "Form has been submitted successfully" appear underneath the form once submitted, I would like it to go to a "Thank You" page instead.

Here is my code:

This is this my form page:

<?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>

And my validate.php file:

<?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 ####
#####################################
}

I am a novice and have used scripts from other places to do what I wanted to do - so be nice lol

Thanks

Chris

解决方案

In my PHP code I always use header("refresh:0;url=the_url_here"); instead of header('Location: url');.

Try it, maybe it works to you.

这篇关于在表单提交并使用php进行验证后,重定向到新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆