2011-10-06 7 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 
} 
?> 

여기에 자바 스크립트입니다 :

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(); 
} 
} 

사용자가 전송 버튼을 클릭 할 때 그 기능을 실행하고 모든 필드가 작성되었는지 확인할 것 후. 나는 자바 스크립트가 활성화되어 있지 않은 것처럼 HTML fallback으로 리다이렉트를 계속하기 때문에 if (isset ($ _ POST [ 'ajax']))라고하는 php의 끝 부분에서 트리핑이 일어났다 고 생각한다. 길). 여기에서 실제 예제를 볼 수 있습니다 : mattsandersdesign.com/test/contact2.html 미리 감사드립니다!

답변

0

나는 마침내 그것을 알아 냈다. 첫째로 잘못된 시간에 false를 반환한다고했습니다. submitForm 함수 외부에서 실행해야했습니다. 둘째로 나는 잘못된 장소에 몇 가지 변수가있다.

0

모든 유효성 검사가 통과 되더라도 return false이어야합니다. 그렇지 않으면 양식이 제출됩니다.
submitForm()으로 전화하고 계십니까?

+0

submitForm()의 맨 아래에 return false가 있지만 어쩌면 다시 그 함수에서 다시해야한다. 예, 나는 모든 필드가 작성되었는지 확인한 다음 submitForm()을 실행 한 후 제출 버튼을 클릭 할 때 실행되는 함수를 가지고 있습니다. – Matt

관련 문제