PHP email form with switches using AJAX to send

TL DR: change .val to javascript for .serialize for any radio inputs.

I used this tutorial to create a form that, when you click the submit button, fades out and disappears in “thank you” and sends mailer.php in the background. My form has radio buttons, and I can’t figure out how to get javascript to send the button that was selected to my email address.

Here's the html form:

<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>

And 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!");

?>

Thank you so much for any information you can provide.

+5
source share
4 answers

. . 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>
+4

:

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

You don't need javascript to get the values ​​so that they can be emailed. Use PHP instead.

   $formcontent .= $_POST['rsvp'];

This line will be added before $ recipient should send the value of the switches.

0
source

change var rsvps = $(".rsvps").val();tovar rsvps = $(".rsvps[selected=selected]").val();

In addition, the dataString should be a json object like this var dataString = { rsvps : rsvps };, accessible via ajax POST.

0
source

All Articles