TL; DR 解决方案:将 javascript 中的 .val 更改为 .serialize 以用于任何无线电输入。
我一直在使用本教程构建一个表单,当按下提交按钮时,按钮淡出并淡出“谢谢”消息并在后台发送 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!");
?>
非常感谢您提供的任何见解。