2015-05-13 68 views
0

我在這裏看過一些解決方案,嘗試了不同的方法來做這件事,但都沒有爲我工作。在PHP驗證後提交表單後顯示感謝信息

這裏是我的形式:

<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("/^\[email protected]\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 protected]"; 
$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"); 

} 
    }} 

?> 
+0

和你有什麼問題,或沒有結果? –

+0

我曾嘗試使用innerHTML來更改內容,但它沒有任何結果。 – Jordan

+0

在哪裏分配了$ errorMsg? –

回答

1

某處,希望在HTML上面,你正在處理的表單提交。所以你應該在那裏設置$ errorMsg。

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

相關問題