【发布时间】:2016-07-19 12:24:34
【问题描述】:
我正在使用 jQuery 来验证有两个选择的简单表单
1-个月(列出月份名称,值为月份编号)
2 年(从 2016 年到 2022 年)
我想从选定的月份和年份中检查数据库中是否有记录, Mysql 表有单独的月份和年份列。
例如:
如何使用远程检查检查 2016 年 1 月是否已在数据库中?
remote.php 是
$inspection_month = $_POST['sm'];
$inspection_year = $_POST['sy'];
$check_for_the_report = $db->single("SELECT id FROM dg_inspection_forms WHERE inspection_month = :sm AND inspection_year = :sy ",array("sm"=>"$inspection_month","sy"=>"$inspection_year"));
if($check_for_the_report){
echo "false";
} else {
echo "true";
}
表单验证部分:
$('.btn-new-inspection-report-save').on('click', function(e){
e.preventDefault();
$("#newInspectionReportFormStep1").validate({
highlight: function(element) {
$(element).closest('.form-group').addClass('has-error');
},
unhighlight: function(element) {
$(element).closest('.form-group').removeClass('has-error');
},
errorElement: 'span',
errorClass: 'help-block',
errorPlacement: function(error, element) {
if(element.parent('.input-group').length) {
error.insertAfter(element.parent());
} else {
error.insertAfter(element);
}
}
});
if($('#newInspectionReportFormStep1').valid()) {
//Check if there is already a report for the selected Month and Year for that clinic
var sm = $('.new_inspection_month').find(':selected').data('fid');
var sy = $('.new_inspection_year').find(':selected').data('yid');
var flag = true;
$.ajax({
type: "POST",
url: '/apps/reports/check-for-previous-reports.php',
data: {sm:sm,sy:sy},
success: function(data) {
if (data === 'false') {
bootbox.alert("There is record for the selected month and year");
flag = false;
}
}
});
if(flag === false){
return flag;
e.preventDefault();
} else {
$('.loading').show();
$.ajax({
type: "POST",
url: '/apps/reports/new-inspection-report-step1-save.php',
data: $("#newInspectionReportFormStep1").serialize(),
success: function(data) {
$('#generateNewInspectionReportStep1').modal('hide');
var appModal = $('#generateNewInspectionReportStep2').modal('show');
appModal.load("/apps/reports/new-inspection-report-step2.php?report_id="+data+"");
$('.loading').hide();
}
});
}
}
});
【问题讨论】:
-
我在这里没有看到任何关于 jQuery Validate 的信息。在询问 jQuery Validate 插件时,请仅使用 jquery-validate 标签。已编辑。谢谢。
标签: php jquery validation