2014-02-20 75 views
0

我有一個PHP聯繫表單,目前將您帶到一個單獨的頁面。我希望「成功/錯誤消息」出現在jQuery對話框中,而不是將您重定向到另一個頁面。或者,如果我可以將它定位到DIV並在提交時顯示它,我只需要在iframe以外的同一頁面上顯示結果。如何在jQuery對話框中顯示PHP結果

<?php 

if(isset($_POST['email'])) { 



// EDIT THE 2 LINES BELOW AS REQUIRED 

$email_to = "[email protected]"; 

$email_subject = "Message From Testing.com"; 





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['name']) || 

    !isset($_POST['email']) || 

    !isset($_POST['telephone']) || 

    !isset($_POST['message'])) { 

    died('We are sorry, but there appears to be a problem with the form you  submitted.');  

} 



$name = $_POST['name']; // required 

$email = $_POST['email']; // required 

$telephone = $_POST['telephone']; // not required 

$message = $_POST['message']; // 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)) { 

$error_message .= 'The Email Address you entered does not appear to be valid.<br />'; 

} 

$string_exp = "/^[A-Za-z .'-]+$/"; 

if(!preg_match($string_exp,$name)) { 

$error_message .= 'The First Name you entered does not appear to be valid.<br />'; 

} 

if(strlen($message) < 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 .= "Name: ".clean_string($name)."\n"; 

$email_message .= "Email: ".clean_string($email)."\n"; 

$email_message .= "Telephone: ".clean_string($telephone)."\n"; 

$email_message .= "Message: ".clean_string($message)."\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); 

?> 



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

<div id="dialog" title="Thank You"> 
<p>Thank you for contacting us. We will be in touch with you very soon.</p> 
</div> 

</html> 

<?php 

} 

?> 

這是我的HTML。

<form name="contactform" method="get" autocomplete="on"> 
        <input type="text" class="contact-form-  input" name="name" placeholder="Name"> 
        <input type="text" class="contact-form-input" name="email" placeholder="Email"> 
        <input type="text" class="contact-form-input" name="telephone" placeholder="Phone"> 
        <textarea rows="5" cols="50" class="contact-form-textarea" name="message" placeholder="Message"></textarea> 
        <input type="button" class="form-submit-button" name="send" onclick="mail();" value="Send"> 
        <input type="reset" class="form-submit-button" name="clear" value="Clear"> 
</form> 
+0

你可以添加HTML和JQuery嗎? php文件不是必需的 – Netzach

+0

@Netzach我剛剛寫完一個評論,詢問同樣的事情。 – NobleUplift

回答

0

您不能在提交表單後執行此操作,您必須在提交表單之前執行此操作。將onClick事件添加到指向具有AJAX請求的函數的窗體中。然後顯示成功的彈出窗口。

<script type="text/javascript"> 
function mail() { 
    var email = jQuery("#email").val(); 
    var name = jQuery("#name").val(); 
    var telephone = jQuery("#telephone").val(); 
    var message = jQuery("#message").val(); 
    var postData = { email: email, name: name, telephone: telephone, message: message }; 
    jQuery.post('mail.php', 
     postData, 
     function (jsonResults) { 
      jQuery("#dialog").show(); 
     }, 
     'text' 
    ); 
} 
</script> 

<form id="mail-form" method="get"> 
    ... 
    <input type="button" value="Get" onclick="mail();" /> 
</form> 
+0

我發佈了我的HTML,我使用了你發佈的Jquery,它沒有做任何事情。 – Colby

+0

是否將對話框移動到與HTML相同的頁面,然後隱藏它? – NobleUplift

+0

我放置了您發佈到我的HTML文件的腳本。沒有沒有隱藏對話框。 – Colby