2010-08-29 61 views
1

這裏是php mailer.php文件獲取空的電子郵件

<?php 

$to = "[email protected]"; 
$subject = "Contact via website"; 
$name_field = $_REQUEST['name']; 
$email_field = $_REQUEST['email']; 
$message = $_REQUEST['message']; 

$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message"; 

mail($to, $subject, $body); 

?> 

這裏是jQuery代碼

$('.submit').click(function(){ 
      $('span.msg').css({'visibility': 'visible'}).text('Sending...'); 
      $.post("mailer.php", $(".contactPage").serialize(), 
       function(data){ 
        $('span.msg').text('Your Message has been received. Thank you').show(); 
       }); 

      return false;  

這裏是HTML代碼

<div class="contactPage"> 
    <label>Name</label> 
    <input type="text" name="name" class="txt" /> 
    <label>Email</label> 
    <input type="text" name="email" class="txt" /> 
    <label>Message</label> 
    <textarea class="txt_area" name="message" rows="5" cols="30"></textarea>   
    <input type="button" class="submit" value="" /> 
    <span class="msg">Your Message has been received. Thank you</span> 
</div> 
     }); 

但我得到空的電子郵件.. ..

+1

你有沒有使用Firebug檢查數據是否被髮送到服務器? – 2010-08-29 23:13:22

回答

3

.serialize()級只有一個<form>元素的作品,所以你需要替換此:

<div class="contactPage"> 

有了這個(和匹配的結束標記):

<form class="contactPage"> 

而且使用submit事件是安全的,像這樣:

$('.contactPage').submit(function(){ 
    $('span.msg').css({'visibility': 'visible'}).text('Sending...'); 
    $.post("mailer.php", $(this).serialize(), function(data){ 
    $('span.msg').text('Your Message has been received. Thank you').show(); 
    }); 
    return false; 
}); 

Here's a demo showing how serializing <div> doesn't work, but a <form> does :)

1

調試這樣的東西的最好方法是安裝firebughttpfox並查看它們發送時的標題。如果您是開發人員,我建議使用螢火蟲,因爲它有很多有用的工具。

我想你會發現,這是該行

$.post("mailer.php", $(".contactPage").serialize(),