2011-02-19 6 views
4

좋아, 긴 이야기를 짧게 만들려면 :jQuery/PHP 메일은 쉬운 방법을 보냅니 까?

자바 스크립트 :

jQuery("submit").click(function() { 

    var address = jQuery("#mail").val(); 
    var title = jQuery("#title").val(); 
    var name = jQuery("#name").val(); 
    var mail = jQuery("#email").val(); 
    var message = jQuery("#msg").val(); 

    alert(address); 
    alert(title); 
    alert(name); 
    alert(mail); 
    alert(message); 

    jQuery.post("sendmail.php", 
    {address: address, title: title, name: name, mail: mail , message: message}, 
    function(data){ 
     jQuery("#email").html("<div id='sent'><p>Thank you for the message.</p><p>We will reply as soon as possible.</p></div>"); 
     alert('sent'); 
    });  

return false;  
}); 

원활하게 작동 (모든 값을 경고에 표시) div "보낸"표시하지 마십시오. 그리고 실제 우편물을 보내지 않습니다.

sendmail.php가 올바른 위치에 있고 코드가 다음과 같습니다.

<?php 

// getting variables from form 

$emailTo = trim($_REQUEST['address']); 
$subject = trim($_REQUEST['title']);; 
$name = trim($_REQUEST['name']); 
$emailFrom = trim($_REQUEST['mail']); 
$message = $_REQUEST['message']; 

// prepare email body text 

$Body = "You have a message from: "; 
$Body .= $name; 
$Body .= "\n"; 
$Body .= "\n"; 
$Body .= $message; 

// send prepared message 

$sent = mail($emailTo, $subject, $Body); 

//callback for jQuery AJAX 

if ($sent){ 
    echo 'sent'; 
} 
else{} 

print_r($_REQUEST); die(); 
?> 

업데이트 된 jQuery 코드도 작동하지 않습니다.

jQuery("#contact-form-send").click(function() {  

    var address = jQuery("#contact-form-mail-address").val(); 
    var title = jQuery("#contact-form-mail-title").val(); 
    var name = jQuery("#contact-form-name").val(); 
    var mail = jQuery("#contact-form-email").val(); 
    var message = jQuery("#contact-form-message").val(); 

    var data = "&address=" + address + "&title=" + title + "&name=" + name + "&mail=" + mail + "&message=" + message; 

    jQuery.ajax({ 
    type: "POST", 
    url: "sendmail.php", 
    data: data, 
    success: function(){ 
      jQuery(".email-us").html("<div id='email-sent'><p>Thank you for the message.</p><p>We will reply as soon as possible.</p></div>");  
    } 
}); 

return false;  
}); 
+0

@ kjy112, 물론, 나는 그것에 대한 작업을하고 있습니다 :) 나는 완전히 첫 번째 게시물을 업데이트했습니다. 아마 문제를 찾을 수있을 것입니다. 하지만 우선 저는 .ajax로 메소드를 검사 할 것입니다. – anonymous

+0

당신은 else 문을 그 안에 아무것도 넣지 않고 닫았습니다 ... 아마도'print_r ($ _REQUEST);'? – kjy112

+0

@ kjy112, sendmail.php 출력에 액세스 할 수없는 방식으로 아무 것도 변경하지 않고 "submit"을 클릭하면 div가 나타납니다. 아무것도 더. sendmail.php 로의 리다이렉트는 없다. – anonymous

답변

5

당신은 Ajax를 사용해야합니다. 그것 훨씬 쉽습니다.

Send mail using PHP, Ajax and jQuery

가이 비슷한을 보일 것이다

가 :

당신의 PHP 파일 또한
var data = "address=" + address + "&title=" + title + "&name=" + name + "&mail=" + mail + "&message=" + message; 
//or however u want to format your data 

$.ajax({ 
    type: "POST", 
    url: "sendmail.php", 
    data: data, 
    success: function(phpReturnResult){ 
      alert('Success: ' + phpReturnResult); 
      jQuery(".email-us").html("<div id='email-sent'><p>Thank you for the message.</p><p>We will reply as soon as possible. PHP Script's return result: " + phpReturnResult + "</p></div>");  
    }, 
    error: function(errormessage) { 
      //you would not show the real error to the user - this is just to see if everything is working 
      alert('Sendmail failed possibly php script: ' + errormessage); 
    } 
}); 

, 당신이 보인다 다음은 간단한 튜토리얼은 PHP, 아약스, & jQuery를 사용하여 메일을 보내는 여기에있다 메일 기능을 두 번 사용했습니다.

+0

Af! 문제는 "성공"이 결코 발생하지 않습니다! "이메일 전송"이라는 div를 보지 못했고 " 고맙습니다 ... "텍스트 :(그게 요점입니다. 모든 것이 잘 작동하고 모든 데이터가 저장되며 링크가 생성되고 sendmail.php 파일이 올바른 위치에 있습니다. (솔직히 말해서 나는 거의 복사했습니다. 어디에도 없음) – anonymous

+0

@anonymous이 오류 기능을 첨부하여 표시되는지 확인합니다. 문제가있는 PHP 스크립트. 그러나 그렇다면 아약스가 성공했음을 의미합니다. – kjy112

+0

@anonymous 성공 또는 실패 모두에 대해 경고가 추가되었으므로 아약스가 실패했는지 아니면 PHP 스크립트인지 알 수 있습니다. 행운을 빕니다 – kjy112

관련 문제