在PHP验证后提交表单后显示谢谢消息

时间:2015-05-13 19:59:23

标签: javascript php html5 forms

我在这里尝试了一些解决方案尝试不同的方法,但没有一个适合我。

这是我的表格:

<form action="<?PHP echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST" name="contact" role="form">

 <?PHP  //if error has occured then echo the error in red
  if(isset($errorMsg) && $errorMsg) {
echo "<p style=\"color: red;\">*",htmlspecialchars($errorMsg),"</p>\n\n";
  }
?>

<label for="name"><b>Name:</b></label>
<input type="text" name="name" id="name" placeholder="Enter full name" value="<?PHP if(isset($_POST['name'])) echo htmlspecialchars($_POST['name']); ?>">   
<label for="email"><b>Email:</b></label>
<input type="text" name="email" id="email" placeholder="Enter a valid email address..." value="<?PHP if(isset($_POST['email'])) echo htmlspecialchars($_POST['email']); ?>">

<label><b>Subject:</b></label>
    <input type="text" name="subject" id="subject" placeholder="Please enter a subject matter" value="<?PHP if(isset($_POST['subject'])) echo htmlspecialchars($_POST['subject']); ?>">
<label for="query"><b>Query:</b></label>
<textarea id="query" placeholder="Please write your message here..." name="query" value="<?PHP if(isset($_POST['query']))echo htmlspecialchars($_POST['query']);?>">
 </textarea>

<input type="submit" name="submit" id="submit" value="Submit" class="style-button"> 

</form>     

我使用的是单页网站,因此联系表单位于页面底部。

如果表单在提交并经过验证后如何在表单中显示感谢信息 - 如果页面没有返回顶部?

PHP代码:

<?php

// if submit is clicked then assign variables
  if($_POST && isset($_POST['submit'], $_POST['name'], $_POST['email'],               $_POST['subject'], $_POST['query'])) {
     $name = $_POST['name'];
     $email = $_POST['email'];
$subject = $_POST['subject'];
$query = $_POST['query'];   

 //making sure the page goes to the contact form with the errors instead of the top of the page
     if(!isset($_POST['$errorMsg'])) 
     {
    ?>
    <script>
    window.location.hash = '#contact-form';
    </script>
    <?php
}


 // if name is not entered then display errorMsg    
 if (!$name) {
        $errorMsg = "Please enter your name";
 }

 // if email is not entered then display errorMsg
 elseif (!$email || !preg_match("/^\S+@\S+$/", $email)) {
 $errorMsg = "Please enter a valid email address";
  }

 // If the subject has not entered then display errorMsg    
 elseif (!$subject) {
 $errorMsg = "Please enter a subject";
  }


 // if query is not entered then display errorMsg    
  elseif (!$query) {
 $errorMsg = "Please enter your query";

  }

  else {
      //send email and redirect to confirmation page

 $toemail = "email@example.com";
 $subject2 = "Message recieved from ". $name."";
 $headers = "MIME-Version: 1.0\n"
 ."From: \"".$name."\" <".$email.">\n"
 ."Content-type: text/html; charset-iso-8859-1\n";
 $body = "Email: ".$email."<br>\n"
 ."Subject: ".$subject."<br>\n"
 ."email: ".$email."<br>\n"
 ."query: ".$query."<br>\n"
 ;
 mail($toemail, $subject, $body, $headers);
 if(mail($toemail, $subject, $body, $headers)){
$toemail =".$email";
$subject = "Confirmation Email";
$body = "Your email has been sent";
header("location: index.php");

 }
  }}

 ?> 

1 个答案:

答案 0 :(得分:1)

某处,希望在HTML之上,您正在处理表单帖子。所以你应该在那里设置$ errorMsg。

自提交网页的经验法则:视图前的逻辑。另外,要避免使用if()语句,请在每次加载页面时初始化$ errorMsg。