获取空电子邮件

时间:2010-08-29 23:10:26

标签: php jquery

这里是php mailer.php文件

<?php

$to = "abc@gmail.com";
$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>
        });

但我收到了空邮件......

2 个答案:

答案 0 :(得分: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 :(得分:1)

调试此类内容的最佳方法是安装firebughttpfox并查看发送时的标头。如果你是开发人员,我推荐使用firebug,因为它有很多有用的工具。

我想你会发现它就是这条线

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