2013-04-12 42 views
1

我需要刪除此表單底部的代碼提交,但是當我刪除代碼時,它會破壞表單。
我希望表單在不重定向到html頁面的情況下提交。停止PHP表單提交從重定向到不同的頁面

<?php 
if(isset($_POST['email'])) { 

    // EDIT THE 2 LINES BELOW AS REQUIRED 
    $email_to = "[email protected]"; 
    $email_subject = "Customer From XzamCorpResults"; 


    function died($error) { 
     // your error code can go here 
     echo "We are very sorry, but there were error(s) found with the form you submitted. "; 
     echo "These errors appear below.<br /><br />"; 
     echo $error."<br /><br />"; 
     echo "Please go back and fix these errors.<br /><br />"; 
     die(); 
    } 

    // validation expected data exists 
    if(!isset($_POST['first_name']) || 
     !isset($_POST['email']) || 
     !isset($_POST['telephone']) || 
     !isset($_POST['comments'])) { 
     died('We are sorry, but there appears to be a problem with the form you submitted.');  
    } 

    $first_name = $_POST['first_name']; // required 
    $email_from = $_POST['email']; // required 
    $telephone = $_POST['phone']; // not required 
    $comments = $_POST['comments']; // required 

    $error_message = ""; 
    $email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; 
    if(!preg_match($email_exp,$email_from)) { 
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />'; 
    } 
    $string_exp = "/^[A-Za-z .'-]+$/"; 
    if(!preg_match($string_exp,$first_name)) { 
    $error_message .= 'The First Name you entered does not appear to be valid.<br />'; 
    } 

    if(strlen($comments) < 2) { 
    $error_message .= 'The Comments you entered do not appear to be valid.<br />'; 
    } 
    if(strlen($error_message) > 0) { 
    died($error_message); 
    } 
    $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 .= "First Name: ".clean_string($first_name)."\n"; 
    $email_message .= "Email: ".clean_string($email_from)."\n"; 
    $email_message .= "Telephone: ".clean_string($telephone)."\n"; 
    $email_message .= "Comments: ".clean_string($comments)."\n"; 


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

***<!-- include your own success html here --> 

Thank you for contacting XzamCorp. We are looking forward to speaking with you about your inquiry, we will contact you as soon as possible. 

<?php 
} 
?>*** 

<form name="contactform" method="post" action="send_form_email.php"> 
<table width="100%" border="0" cellpadding="3" cellspacing="0"> 
<p align="right"> 
<tr> 
<td valign="top"> 
    <input class="text" type="text" name="first_name" id="first_name" value="Name..." onfocus="if(this.value=='Name...')this.value='';" onblur="if(this.value=='')this.value='Name...';" /> </td> 
</tr> 
<tr> 
<td valign="top"> 
    <input class="text" type="text" name="email" id="email" value="Email..." onfocus="if(this.value=='Email...')this.value='';" onblur="if(this.value=='')this.value='Email...';" /> 
</td> 
</tr> 
<tr> 
<td valign="top"> 
    <input class="text" type="text" name="telephone" id="telephone" value="Phone..." onfocus="if(this.value=='Phone...')this.value='';" onblur="if(this.value=='')this.value='Phone...';" /> 
</td> 
</tr> 
<tr> 
<td valign="top"> 
<textarea rows="10" cols="10" type="text" name="comments" id="comments" onfocus="if(this.value=='Your Feedback...')this.value='';" onblur="if(this.value=='')this.value='Your Feedback...';" >Your Feedback...</textarea> 
</td> 
</tr> 
<tr> 
<td colspan="2" style="text-align:"> 
    <input type="submit" value="Submit"> 
</td> 
</tr> 
</table> 
</form> 
+0

你能指出你想要刪除的特定塊嗎? –

+0

在頁面的最底部,開始點和結束點用*** –

+0

標記,你不能刪除它,因爲最後一個PHP位中的大括號。刪除其他的東西,你應該沒問題。 –

回答

1

您不能刪除該代碼,因爲您的成功HTML後面的PHP標籤之間的大括號。你應該沒問題,如果你刪除其他的東西。

注:如果要繼續顯示成功的消息時,發送的電子郵件,但沒有重定向,忽略了我在下面的代碼塊所做的更改,使我在我的更新中提到的變化, 在底部。

<?php 
if(isset($_POST['email'])) { 

    // EDIT THE 2 LINES BELOW AS REQUIRED 
    $email_to = "[email protected]"; 
    $email_subject = "Customer From XzamCorpResults"; 


    function died($error) { 
     // your error code can go here 
     echo "We are very sorry, but there were error(s) found with the form you submitted. "; 
     echo "These errors appear below.<br /><br />"; 
     echo $error."<br /><br />"; 
     echo "Please go back and fix these errors.<br /><br />"; 
     die(); 
    } 

    // validation expected data exists 
    if(!isset($_POST['first_name']) || 
     !isset($_POST['email']) || 
     !isset($_POST['telephone']) || 
     !isset($_POST['comments'])) { 
     died('We are sorry, but there appears to be a problem with the form you submitted.');  
    } 

    $first_name = $_POST['first_name']; // required 
    $email_from = $_POST['email']; // required 
    $telephone = $_POST['phone']; // not required 
    $comments = $_POST['comments']; // required 

    $error_message = ""; 
    $email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; 
    if(!preg_match($email_exp,$email_from)) { 
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />'; 
    } 
    $string_exp = "/^[A-Za-z .'-]+$/"; 
    if(!preg_match($string_exp,$first_name)) { 
    $error_message .= 'The First Name you entered does not appear to be valid.<br />'; 
    } 

    if(strlen($comments) < 2) { 
    $error_message .= 'The Comments you entered do not appear to be valid.<br />'; 
    } 
    if(strlen($error_message) > 0) { 
    died($error_message); 
    } 
    $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 .= "First Name: ".clean_string($first_name)."\n"; 
    $email_message .= "Email: ".clean_string($email_from)."\n"; 
    $email_message .= "Telephone: ".clean_string($telephone)."\n"; 
    $email_message .= "Comments: ".clean_string($comments)."\n"; 


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

} 
?> 

以上應該可以正常工作。


UPDATE

這裏是你想做的事 - 某處的形式在頁面上,插入以下到您的PHP代碼:

include("send_form_email.php"); 

然後改變你<form>標記爲:

<form name="contactform" method="post" action=""> 

它會回發到同一頁面。如果您希望顯示成功消息,但只有在成功發送電子郵件時,請使用您發佈的原始代碼,而不是上面大代碼塊中的編輯代碼。

+0

好吧我改變它到上面,它確實工作正常,但它仍然加載實際的.php文件在瀏覽器中顯示一個空白屏幕,而不是成功消息。 –

+0

這裏看一看http://www.xzamcorpresults.com/company/results.php其右側的表格 –

+1

我只是希望表單提交時沒有任何事情發生在瀏覽器上 –

0

對不起,只是爲了澄清「include("send_form_email.php");」去形式之間的頁面,<script></script>謝謝!

這個問題一直在竊聽我一下,看到這裏:candu-projects.org

相關問題