PHP表单提交到自己无法加载

时间:2014-08-08 14:08:25

标签: php validation session-variables

我通过添加验证码来定制php表单代码,我希望在表单上显示错误,而不是使用die命令将其显示到另一个页面或弹出对话框。我使用PHP sesssion而不是Ajax。我遇到了以下问题。

1)单击提交按钮后,浏览器地址栏显示处理php文件路径,但无法在header命令后再次加载表单,只显示空白屏幕。以前,它能够加载带有错误代码的表单,并且验证码工作正常。我不知道出了什么问题。

2)奇怪的是,如果我按下浏览器后退按钮,表单就会加载。当我重新加载表单再次启动时,它不是清除表单,而是开始显示错误消息,但一次只显示一条错误消息! (但是如果多个字段为空或填充了错误的字符类型,我想立即显示所有错误消息。)为什么表单会在刷新页面时处理数据?不应该只在按下提交按钮时发生吗?

3)尽管存在加载问题,我仍然收到空白字段的电子邮件!

请帮忙。

contactform.php代码如下:

<?php
session_start();
?>
<form action="php/contact.php" method="post" id="frmContact">

<h5 style="color: #ffffff;">What's your Name?</h5>
<input name="txtName" type="text" class="txbx" value="" />
<?php echo $_SESSION[lmsg_name]; ?><br />

<h5 style="color: #ffffff;">What's your Email?</h5>
<input name="txtEmail" type="text" class="txbx" value="" />
<?php echo $_SESSION[lmsg_email]; ?><br />

<h5 style="color: #ffffff;">Email Subject?</h5>
<input name="txtSubject" type="text" class="txbx" value="" />
<?php echo $_SESSION[lmsg_subject]; ?><br />

<h5 style="color: #ffffff;">Message to us</h5>
<textarea name="txtText" class="txbx era" placeholder="" ></textarea>
<?php echo $_SESSION[lmsg_text]; ?><br />

<h5 style="color: #ffffff;">Enter verification code</h5>
<img src="php/captcha.php" style="float:left; margin-left: 11px; margin-right: 10px;   margin-top: 5px;" >
<input type="text" name="vercode" class="txbxcap" />
<?php echo $_SESSION[lmsg_incode]; ?><br />

<input name="" type="submit" class="sendbtn" value="SEND MESSAGE" id="btnSend"/>

</form>
<?php
$_SESSION[lmsg_name] ='';
$_SESSION[lmsg_email] ='';
$_SESSION[lmsg_subject] ='';
$_SESSION[lmsg_text] ='';
$_SESSION[lmsg_incode] ='';
?>

captcha.php代码如下:

<?php 
session_start(); 
$text = rand(10000,99999); 
$_SESSION["vercode"] = $text; 
$height = 38; 
$width = 55; 

$image_p = imagecreate($width, $height); 
$black = imagecolorallocate($image_p, 0, 0, 0); 
$white = imagecolorallocate($image_p, 255, 255, 255); 
$font_size = 16; 

imagestring($image_p, $font_size, 5, 5, $text, $white); 
imagejpeg($image_p, null, 80); 
?>

处理contact.php如下:

<?php

session_start(); 

$Name = htmlspecialchars($_POST['txtName']);
$Email = htmlspecialchars($_POST['txtEmail']);
$Subject = htmlspecialchars($_POST['txtSubject']);
$Text = htmlspecialchars($_POST['txtText']);

$string_exp = "/^[A-Za-z .'-]+$/";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';


if (empty ($Name) || !preg_match($string_exp,$Name)) { 
$error_name = '<div style="color: #e44558; float:left; margin-left: 11px;">*Please provide a name.<br /></div>';
$_SESSION[lmsg_name] = $error_name;
header('Location:http:../contactform.php');
}

elseif (empty ($Email) || !preg_match($email_exp,$Email)) { 
$error_email = '<div style="color: #e44558; float:left; margin-left: 11px;">*Please enter a valid email address.<br /></div>';
$_SESSION[lmsg_email] = $error_email;
header('Location:http:../contactform.php');
}

elseif (empty ($Subject) ||!preg_match($string_exp,$Subject)) {
$error_subject = '<div style="color: #e44558; float:left; margin-left: 11px;">*Please provide a subject.<br /></div>';
$_SESSION[lmsg_subject] = $error_subject;
header('Location:http:../contactform.php');
}

elseif (empty ($Text) ||!preg_match($string_exp,$Text)) {
$error_text = '<div style="color: #e44558; float:left; margin-left: 11px;">*Please write few words...<br /></div>';
$_SESSION[lmsg_text] = $error_text;
header('Location:http:../contactform.php');
}

elseif ($_POST["vercode"] != $_SESSION["vercode"] OR $_SESSION["vercode"]=='') { 
$error_code = '<div style="color: #e44558; float:left; margin-left: 11px;">*Incorrect verification code. Please try again.<br /></div>';
$_SESSION[lmsg_incode] = $error_code;
header('Location:http:../contactform.php');
}

else {

//create email
$email_to = "x@y.com";
$email_subject = "Visitor Query: ". $Subject;
$email_message = "Form details below.\n\n";

function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}

$email_message .= "Name: ".clean_string($Name)."\n";
$email_message .= "Email: ".clean_string($Email)."\n";
$email_message .= "Message: ".clean_string($Text)."\n";


// create email headers
$headers = 'From: '.$Email."\r\n".
'Reply-To: '.$Email."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);     


header('location:../contact_thnx.html');
}
?>

提前谢谢。

0 个答案:

没有答案