我试图验证是否使用AJAX表单提交选中了复选框。基本上我想确保选中标准费率或紧急费率复选框(因为只有其中一个会从早期的代码中显示)。我和其他人一样尝试过.val(),但它也没有用。此外,选择不起作用。但我的猜测是复选框和选择来自同一个问题。
HTML代码:
<li class="standard-rate rate">
<input type="checkbox" name="acknowledge-standard-rate" value="Standard Rate" id="acknowledge-standard-rate"><div class="standard-rate-desc">(*)I acknowledge time spent on my request will be deducted from my current contract or, in the absence of an hourly pack, billed at the end of the month.</div>
</li>
<li class="emergency-rate rate">
<input type="checkbox" name="acknowledge-emergency-rate" value="Emergency Rate" id="acknowledge-emergency-rate"><div class="emergency-rate-desc">(*)I acknowledge time spent on my request will be billed at our $200/hour emergency rate.</div>
</li>
AJAX / jQuery代码:
var formData = {
'first_name' : $('input[name=first_name]').val(),
'last_name' : $('input[name=last_name]').val(),
'email' : $('input[name=email]').val(),
'phone' : $('input[name=phone]').val(),
'message' : $('textarea[name=message]').val(),
'severity' : $('select[name=severity]').val(),
'acknowledge-standard-rate' : $('input[name=acknowledge-standard-rate]').is(':checked'),
'acknowledge-emergency-rate' : $('input[name=acknowledge-emergency-rate]').is(':checked')
};
// process the form
var request = $.ajax({
type : 'POST', // define the type of HTTP verb we want to use (POST for our form)
url : 'process.php', // the url where we want to POST
data : formData, // our data object
dataType : 'json', // what type of data do we expect back from the server
encode : true
})
来自process.php的PHP代码
if (!isset($_POST['acknowledge-standard-rate']) && !isset($_POST['acknowledge-emergency-rate'])) {// if
//doing null check for checkbox and returning one
$errors['checkbox'] = 'Please check the check box aknowledging payment.';
} // if
答案 0 :(得分:1)
我认为这里的问题是,您始终在javascript中的这两个复选框字段中设置值true or false
。
默认的浏览器表单处理实际上根本不会发送变量,如果没有检查它并且你的PHP代码可以工作。
所以不要总是创建这两个变量
'acknowledge-standard-rate' : $('input[name=acknowledge-standard-rate]').is(':checked'),
'acknowledge-emergency-rate' : $('input[name=acknowledge-emergency-rate]').is(':checked')
};
在javascript中,如果选中相关复选框,则只需创建它们。
也许是这样的
var formData = {
'first_name' : $('input[name=first_name]').val(),
'last_name' : $('input[name=last_name]').val(),
'email' : $('input[name=email]').val(),
'phone' : $('input[name=phone]').val(),
'message' : $('textarea[name=message]').val(),
'severity' : $('select[name=severity]').val()
};
if ( $('input[name=acknowledge-standard-rate]').is(':checked') ) {
formData.acknowledge-standard-rate = $('input[name=acknowledge-standard-rate]').val();
}
if ( $('input[name=acknowledge-emergency-rate]').is(':checked') ) {
formData.acknowledge-emergency-rate = $('input[name=acknowledge-emergency-rate'].val();
}
process the form
var request = $.ajax({
type : 'POST',
url : 'process.php',
data : formData,
dataType : 'json',
encode : true
});
您可能还想在启动此AJAX流程之前验证是否已在javascript中实际检查了其中一个,提供了一个简单的错误对话框,告诉用户选择一个。
答案 1 :(得分:0)
试试这个:
if ($_POST['acknowledge-standard-rate'] == false && $_POST['acknowledge-emergency-rate'] == false) {// if
//doing null check for checkbox and returning one
$errors['checkbox'] = 'Please check the check box aknowledging payment.';
}