【问题标题】:jQuery remote check validationjQuery远程检查验证
【发布时间】: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


【解决方案1】:

您需要为您处理数据库的单独 php 文件

Ajax php 文件应如下所示:

<?php
if(isset($_POST['month'])){
    //Connect to database
    $link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");

    if (!$link) {
        echo "Error: Unable to connect to MySQL." . PHP_EOL;
        echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
        echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
        exit;
    }

    $result = mysqli_query($link, "SELECT * FROM table_name WHERE Month=".$_POST['month']." AND Year=".$_POST['year']);
    $row_count = mysqli_num_rows($result);

}
$arr = array();
$arr['row_count'] = $row_count;
echo json_encode($arr);
exit;

和jquery函数:

function check_form(){
    var monthx = $("#id_of_month_input").val();
    var yearx = $("#id_of_year_input").val();
    $.ajax({
        type: "POST",
        url: "url_to_your_ajax.php",
        data: { month: monthx, year: yearx},
        cache: false,
        success: function(data){
            var arr = $.parseJSON(data);
            var row_count = arr['row_count'];
        }
    });
}

【讨论】:

  • 你的代码和我的几乎一样,但是在我的代码中一切都很好,除非成功数据为假,它不会返回假。
  • 检查您的网址是否正确,因为如果成功不正确,则意味着他们无法找到文件或文件中的错误。尝试在浏览器本身上打开文件并查看它是否出现任何错误
猜你喜欢
  • 1970-01-01
  • 2011-07-07
  • 1970-01-01
  • 2011-09-27
  • 1970-01-01
  • 1970-01-01
  • 2013-07-23
  • 2015-05-17
  • 2013-01-20
相关资源
最近更新 更多