簡體   English   中英

PHP驗證后提交表單后顯示感謝信息

[英]Show thank you message after form is submitted after PHP validation

我在這里查看了一些嘗試以不同方式執行此操作的解決方案,但沒有一個對我有用。

這是我的表格:

<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");

 }
  }}

 ?> 

希望在HTML之上的某個地方,您正在處理表單發布。 因此,您應該在其中設置$ errorMsg。

自提交網頁的經驗法則:查看前的邏輯。 另外,為避免if()語句,請在每次加載頁面時初始化$ errorMsg。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM