我有一个表单,无法使用php检索单选按钮的值。我已经搜索了SO,可以找到很多类似这样的问题,但没有一个答案似乎有帮助。
形式:
<form method="post" action="wpurse.php" name="contactform" id="weddingform">
<fieldset>
<h4>Wedding Details</h4>
<label for="bname" accesskey="A"><span class="required">*</span> Bride’s name</label>
<input name="bname" type="text" id="bname" size="30" value="" />
<label for="gname" accesskey="B"><span class="required">*</span> Groom’s name</label>
<input name="gname" type="text" id="gname" size="30" value="" />
<label for="weddate" accesskey="C"><span class="required">*</span> Wedding date (DD/MM/YYYY)</label>
<input name="weddate" type="text" id="weddate" />
<label for="venue" accesskey="D"><span class="required">*</span> Venue</label>
<input type="text" id="venue" name="venue"/>
<label for="guestsno" accesskey="D"><span class="required">*</span> Number of guests</label>
<input type="text" id="guestsno" name="guestsno"/>
<h4>Bride & Groom Details</h4>
<label for="address" accesskey="E"><span class="required">*</span> Contact address</label>
<input type="text" id="address" name="address" />
<label for="bemail" accesskey="F"><span class="required">*</span> Bride’s email</label>
<input name="bemail" type="text" id="bemail" size="30" value="" />
<label for="bphone" accesskey="G"><span class="required">*</span> Bride’s phone</label>
<input name="bphone" type="text" id="bphone" size="30" value="" />
<label for="gemail" accesskey="H"><span class="required">*</span> Groom’s email</label>
<input name="gemail" type="text" id="gemail" size="30" value="" />
<label for="gphone" accesskey="I"><span class="required">*</span> Groom’s phone</label>
<input name="gphone" type="text" id="gphone" size="30" value="" />
<label>Will you require invitation inserts?</label>
<label for="inserts0"> YES</label>
<input type="radio" id="inserts0" name="inserts" value="Yes" class="inscheck" onclick="toggleAdditionalElements(this.value);"/>
<label for="inserts1"> NO</label>
<input type="radio" id="inserts1" name="inserts" value="No" class="inscheck" onclick="toggleAdditionalElements(this.value);"/>
<div id="AdditionalElements" style="display: none;">
<label for="qty" accesskey="I">Quantity required</label>
<input name="qty" type="text" id="qty" value="" />
</div>
<label for="comments" accesskey="C"><span class="required">*</span> Your comments</label>
<textarea name="comments" cols="40" rows="3" id="comments" style="width: 350px;"></textarea>
<p><span class="required">*</span> Are you human?</p>
<label for="verify" accesskey="V"> 3 + 1 =</label>
<input name="verify" type="text" id="verify" size="4" value="" style="width: 30px;" /><br /><br />
<input type="submit" class="submit" id="submit" value="Submit" />
</fieldset>
</form>
<script type="text/javascript">
function toggleAdditionalElements(val) {
if(val == "Yes")
jQuery("#AdditionalElements").show(200);
else
jQuery("#AdditionalElements").hide(200);
}
</script>
PHP处理:
<?php
if(!$_POST) exit;
function isEmail($bemail) {
return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$bemail));
}
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
$bname = $_POST['bname'];
$gname = $_POST['gname'];
$weddate = $_POST['weddate'];
$venue = $_POST['venue'];
$guestsno = $_POST['guestsno'];
$address = $_POST['address'];
$bemail = $_POST['bemail'];
$bphone = $_POST['bphone'];
$gemail = $_POST['gemail'];
$gphone = $_POST['gphone'];
$inserts =$_POST['inserts'];
$qty = $_POST['qty'];
$comments = $_POST['comments'];
$verify = $_POST['verify'];
if(trim($bname) == '') {
echo '<div class="error_message">Attention! You must enter the Bride’s name.</div>';
exit();
} else if (trim($gname) == '') {
echo '<div class="error_message">Attention! You must enter the Groom’s name.</div>';
exit();
} else if(trim($weddate) == '') {
echo '<div class="error_message">Attention! Please enter the wedding date (DD/MM/YYYY).</div>';
exit();
} else if(trim($venue) == '') {
echo '<div class="error_message">Attention! Please enter the wedding venue.</div>';
exit();
} else if(trim($guestsno) == '') {
echo '<div class="error_message">Attention! Please enter an approx number of guests.</div>';
exit();
} else if(trim($address) == '') {
echo '<div class="error_message">Attention! Please enter the best postal address to contact the Bride and/or Groom.</div>';
exit();
} else if(trim($bemail) == '') {
echo '<div class="error_message">Attention! Please enter a valid email address for the Bride.</div>';
exit();
} else if(trim($bphone) == '') {
echo '<div class="error_message">Attention! Please enter a valid phone number for the Bride.</div>';
exit();
} else if(trim($gemail) == '') {
echo '<div class="error_message">Attention! Please enter a valid email address for the Groom.</div>';
exit();
} else if(trim($gphone) == '') {
echo '<div class="error_message">Attention! Please enter a valid phone number for the Groom.</div>';
exit();
} else if(!is_numeric($bphone)) {
echo '<div class="error_message">Attention! The Bride’s phone number can only contain digits.</div>';
exit();
} else if(!is_numeric($gphone)) {
echo '<div class="error_message">Attention! The Groom’s phone number can only contain digits.</div>';
exit();
} else if(!isEmail($bemail)) {
echo '<div class="error_message">Attention! You have enter an invalid e-mail address for the Bride, try again.</div>';
exit();
} else if(!isEmail($gemail)) {
echo '<div class="error_message">Attention! You have enter an invalid e-mail address for the Groom, try again.</div>';
exit();
} else if (trim($inserts) == '') {
echo '<div class="error_message">Attention! Please indicate if you will require invitation inserts</div>';
exit();
} else if(trim($subject) == '') {
echo '<div class="error_message">Attention! Please enter a subject.</div>';
exit();
} else if(trim($comments) == '') {
echo '<div class="error_message">Attention! Please enter your message.</div>';
exit();
} else if(!isset($verify) || trim($verify) == '') {
echo '<div class="error_message">Attention! Please enter the verification number.</div>';
exit();
} else if(trim($verify) != '4') {
echo '<div class="error_message">Attention! The verification number you entered is incorrect.</div>';
exit();
}
if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}
$address = "email@address.com";
$e_subject = 'Wedding purse registration - ' . $bname . '.';
$e_body = "Details below" . PHP_EOL . PHP_EOL;
$e_content = "\"$comments\"" . PHP_EOL . PHP_EOL;
$e_reply = "You can contact $name via email, $bemail or via phone $bphone";
$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );
$headers = "From: $bemail" . PHP_EOL;
$headers .= "Reply-To: $bemail" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
if(mail($address, $e_subject, $msg, $headers)) {
echo "<fieldset>";
echo "<div id='success_page'>";
echo "<h1>Email Sent Successfully.</h1>";
echo "<p>Thank you <strong>$bname</strong>, your message has been submitted to us.</p>";
echo "</div>";
echo "</fieldset>";
} else {
echo 'ERROR!';
}
Jquery的:
jQuery(document).ready(function(){
$('#weddingform').submit(function(){
var action = $(this).attr('action');
$("#message_box").slideUp(750,function() {
$('#message_box').hide();
$('#submit')
.after('<img src="assets/ajax-loader.gif" class="loader" />')
.attr('disabled','disabled');
$.post(action, {
bname: $('#bname').val(),
gname: $('#gname').val(),
weddate: $('#weddate').val(),
venue: $('#venue').val(),
guestsno: $('#guestsno').val(),
address: $('#address').val(),
bemail: $('#bemail').val(),
bphone: $('#bphone').val(),
gemail: $('#gemail').val(),
gphone: $('#gphone').val(),
inserts: $('#inserts').val(),
qty: $('#qty').val(),
comments: $('#comments').val(),
verify: $('#verify').val()
},
function(data){
document.getElementById('message_box').innerHTML = data;
$('#message_box').slideDown('slow');
$('#contactform img.loader').fadeOut('slow',function(){$(this).remove()});
$('#submit').removeAttr('disabled');
if(data.match('success') != null) $('#contactform').slideUp('slow');
}
);
});
return false;
});
});
我知道它可能是toggle issue,但即使我切换收音机,我仍然会“没有选择”。
没有单选按钮,表单可以正常工作,所以问题肯定在于它们。
答案 0 :(得分:2)
inserts: $('#inserts').val(),
那就是你的问题。您没有ID为“插入”的输入。
答案 1 :(得分:1)
您必须在jQuery表单提交部分中使用inserts: $('input[name=inserts]:checked').val(),
。
这应该有效。试试这个。
您无法按ID选择广播组,因为有许多单选按钮。通过使用ID,它将只选择一个,而不是选择无线电组。单选按钮将在一个组中,并在组内共享一个公用名。所以我们可以使用基于上面名称的选择器。