在使用JSON时,我无法确定选中了哪个复选框。
这是我的HTML
<form id="patronReg" action="" method="get" onsubmit="jsquicksub(this); return false;">
<input type="checkbox" name="__gold" value="gold" id="patron-gold"/><strong>Gold patrons </strong>$3,000 (+GST for businesses)<br />
<input type="checkbox" name="__silver" value="silver" id="patron-silver" /><strong>Silver patrons </strong>$1,500 (+GST for businesses)<br />
<input type="checkbox" name="__families" value="families" id="patron-families" /><strong>Families and individual </strong>$500<br />
我的JSON javascript文件看起来像这样
function jsquicksub(po_form) {
if (gb_ajaxbusy)
return false;
gb_ajaxbusy = true;
var lo_request = {};
lo_request.g = $(po_form).find('#patron-gold').val();
lo_request.s = $(po_form).find('#patron-silver').val();
lo_request.f = $(po_form).find('#patron-families').val();
$(po_form).fadeTo(200, 0.3);
$.getJSON('/patron-form-process', lo_request, function(po_result) {
gb_ajaxbusy = false;
$(po_form).fadeTo(0, 1);
if (!po_result.success) {
$('#errorReport').show();
document.getElementById('errorReport').innerHTML = po_result.content;
//alert(po_result.content);
return false;
}else{
setTimeout(function() {
$.modal.close();
}, 2500);
}
// replace form with thankyou message
$(po_form).replaceWith(po_result.content);
// empty errrReport element
$('#errorReport').hide();
}); }
我的php表单处理文件就是这个
// function gets passed values after validation and returns a message to the user on screen
function jsonreturn($pb_success, $ps_content = null) {
$lo_result = (object) array('success'=>$pb_success, 'content'=>$ps_content);
ob_clean();
echo json_encode($lo_result);
exit;
} // end jsonreturn()
// membership type
$ps_gold = isset($_GET['g'])? $_GET['g']: null;
$ps_families = isset($_GET['f'])? $_GET['f']: null;
$ps_families = isset($_GET['f'])? $_GET['f']: null;
if ($ps_gold == 'gold') {
$ps_membership = 'Gold patron';
}
elseif ($ps_silver == 'silver') {
$ps_membership = 'Silver patron';
}
elseif ($ps_families == 'families') {
$ps_membership = 'Families and individual';
}
else {
$ps_membership = '';
}
所以我的问题是,当它从表单转到javascript文件然后再转到php时,如何将选中的复选框值添加到php文件中。这可能是一个非常愚蠢的问题!但我对JSON很新。一旦我进入php文件,我使用phpmailer从电子邮件中发送表单中的所有值。它工作得很棒!除了复选框部分!
感谢您的帮助!
答案 0 :(得分:0)
处理此问题的一种方法是使用Jquery Checked Selector来传递已选中的复选框。例如:
// ...
var lo_request = {};
lo_request.g = $(po_form).find('#patron-gold').is(':checked');
lo_request.s = $(po_form).find('#patron-silver').is(':checked');
lo_request.f = $(po_form).find('#patron-families').is(':checked');
$.getJSON('/patron-form-process', lo_request, function(po_result) {
// ...
然后从PHP方面:
$ps_gold = isset($_GET['g'])? $_GET['g'] === 'true' : false;
$ps_silver = isset($_GET['s'])? $_GET['s'] === 'true' : false;
$ps_families = isset($_GET['f'])? $_GET['f'] === 'true' : false;
if ($ps_gold) {
$ps_membership = 'Gold patron';
}
elseif ($ps_silver) {
$ps_membership = 'Silver patron';
}
elseif ($ps_families) {
$ps_membership = 'Families and individual';
}
else {
$ps_membership = '';
}
但是,我要指出,似乎决定$ps_membership
的逻辑似乎更倾向于单选按钮而不是复选框,因为每个选项都是互斥的。话虽如此,上面的解决方案应该做你需要的。