2011-10-06 59 views
0

嘿傢伙我有一個表格,應該發送消息,而無需重新加載頁面。我在這裏使用了這個教程:http://www.elated.com/articles/slick-ajax-contact-form-jquery-php/,並對其進行了調整以適應我的需求。這裏是PHP:AJAX/PHP表格不發送信息

<?php 
session_start(); 
// Define some constants 
define("RECIPIENT_NAME", "John Smith"); 
define("RECIPIENT_EMAIL", "[email protected]"); 
define("EMAIL_SUBJECT", "Visitor Message"); 

// Read the form values and define variables 
$success = false; 
$senderName = isset($_POST['name']) ? preg_replace("/[^\.\-\' a-zA-Z0-9]/", "", $_POST['name']) : ""; 
$senderEmail = isset($_POST['email']) ? preg_replace("/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['email']) : ""; 
$message = isset($_POST['message']) ? preg_replace("/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message']) : ""; 
$honeypot = $_POST['url']; 
$code = $_POST['code']; 

//if the honeypot is filled out, dont send the form 
if (!empty($honeypot)){ 
    $success = false; 
} 
// If all values exist and the code matches, send the email 
else if ($senderName && $senderEmail && $message && $code == $_SESSION['random_code']) { 
$recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">"; 
$headers = "From: " . $senderName . " <" . $senderEmail . ">"; 
$success = mail($recipient, EMAIL_SUBJECT, $message, $headers); 
} 



// Return an appropriate response to the browser 
if (isset($_POST["ajax"])) { 
echo $success ? "success" : "error"; 
} else { 
?> 
<html> 
<head> 
    <title>Thanks!</title> 
</head> 
<body> 
<?php if ($success) echo "<p>Thanks for sending your message! We'll get back to you shortly.</p>" ?> 
<?php if (!$success) echo "<p>There was a problem sending your message. Please try again.</p>" ?> 
<p>Click your browser's Back button to return to the page.</p> 
</body> 
    </html> 
<?php 
} 
?> 

這裏是JavaScript:

function submitForm() { 
var contactForm = $(this); 

//submit the form to the PHP script via Ajax 
$('#sendingMessage').fadeIn(); 

$.ajax({ 
    url: contactForm.attr('action') + "?ajax=true", 
    type: contactForm.attr('method'), 
    data: contactForm.serialize(), 
    success: submitFinished 
}); 


// Prevent the default form submission occurring 
return false; 
} 

function submitFinished(response) { 
response = $.trim(response); 
$('#sendingMessage').fadeOut(); 

if (response == "success") { 

// Form submitted successfully: 
// 1. Display the success message 
// 2. Clear the form fields 

$('#successMessage').fadeIn().delay(5000).fadeOut(); 
$('#name').val(""); 
$('#email').val(""); 
$('#message').val(""); 

} else { 

// Form submission failed: Display the failure message, 
$('#failureMessage').fadeIn().delay(5000).fadeOut(); 
} 
} 

當用戶點擊發送按鈕功能運行,它的檢查,所有的領域都填寫了之後。我認爲這是絆倒在PHP的結尾,它說,如果(isset($ _ POST ['ajax'])),因爲我不斷重定向到html後退,就好像我沒有啓用JavaScript(我通過方式)。你可以在這裏看到一個工作示例:mattsandersdesign.com/test/contact2.html 在此先感謝!

回答

0

好的我終於明白了。首先,我在錯誤的時間打電話回覆錯誤。我需要在submitForm函數之外運行它。其次,我在錯誤的地方有幾個變量

0

即使所有驗證檢查都通過,您也必須return false。否則,表單將提交。
你甚至還打電話submitForm()有史以來?

+0

我已經在submitForm()的底部返回false,但也許我必須再次執行該功能。燁我有一個函數,當用戶點擊提交按鈕時運行,它檢查是否所有字段都已寫入,然後在此之後運行submitForm() – Matt