PHP邮件表格,带有使用AJAX发送的单选按钮

时间:2012-09-23 00:25:06

标签: php javascript ajax phpmailer mailer

TL; DR解决方案:将javascript中的.val更改为.serialize以获取任何无线电输入。

我一直在使用this tutorial来构建一个表单,当按下提交按钮时,淡出按钮并淡入“谢谢”消息并在后台发送mailer.php。我的表单有单选按钮,我似乎无法弄清楚如何通过我的电子邮件将javascript发送到哪个按钮。

这是表单html:

<form action="" method="" name="rsvp" id="rsvp-form">
<fieldset>
                <legend>RSVP</legend>

                    <ol>
                        <li>
                            <input id="accepts1" class="rsvps" name="rsvps" type="radio" value="Graciously_Accepts" />
                            <label for="accepts1">Graciously Accepts</label>
                        </li>
                        <li>
                            <input id="declines1" class="rsvps" name="rsvps" type="radio" value="Regretfully_Declines" />
                            <label for="declines1">Regretfully Declines</label>
                        </li>
                        <li>
                            <input id="accepts2" class="rsvps" name="rsvps" type="radio" value="Regretfully_Accepts" />
                            <label for="accepts2">Regretfully Accepts</label>
                        </li>
                        <li>
                            <input id="declines2" class="rsvps" name="rsvps" type="radio" value="Graciously_Declines" />
                            <label for="declines2">Graciously Declines</label>
                        </li>
                    </ol>
            </fieldset>
<div id="rsvp-wrapper">
    <fieldset>
     <button class="button" type="submit" value="send">RSVP!</button>
</fieldset>

</form>
<div class="success"></div>
</div>

javascript:

<script type="text/javascript">

$(function() {  

$(".button").click(function() {  

var rsvps = $(".rsvps").val();

var dataString = 'rsvps=' + rsvps;  

    $.ajax({  
      type: "POST",  
      url: "rsvp-mailer.php",  
      data: dataString,  
      success: function() {  
        $('#rsvp-wrapper').html("<div class='success'></div>");  
        $('.success').html("<p class='italic'>Thanks!</p>")   
        .hide()  
        .fadeIn(500, function() {  
          $('.success');  
        });  
      }  
    });  
    return false;   
});  
});  

</script>

和mailer.php:

<?php 

$rsvps = $_POST['rsvps'];

$formcontent="

RSVP: $rsvps \n";

$recipient = "myemail@domain.com";

$subject = "RSVP";

$mailheader = "RSVP \r\n";

mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");

?>

非常感谢您提供的任何见解。

3 个答案:

答案 0 :(得分:4)

试一试。有关详细信息,请参阅jQuery.post()

<script type="text/javascript">
    $('form').submit(function() {
        var data = $(this).serialize();

        $.post("rsvp-mailer.php", data, function() {
            $('#rsvp-wrapper').html("<div class='success'></div>");  
            $('.success').html("<p class='italic'>Thanks!</p>")   
            .hide()  
            .fadeIn(500, function() {  
                $('.success');  
            });  
        }

    return false;
    }
</script>

答案 1 :(得分:1)

不要通过类选择器访问单选按钮,而是尝试以下操作:

var rsvps = $('input[name=rsvps]:radio').val();

答案 2 :(得分:0)

var rsvps = $(".rsvps").val();更改为var rsvps = $(".rsvps[selected=selected]").val();

此外,dataString需要是一个像这个var dataString = { rsvps : rsvps };的json对象,可以通过ajax POST访问。