php - jQuery remote check validation -
i using jquery validation simple form has 2 selects
1- months (listing month names , value month number)
2- years (from 2016-2022)
i want check selected month , year whether there record on database, mysql table has seperate month , year column.
for example:
how can check january 2016 in database using remote check?
remote.php is
$inspection_month = $_post['sm']; $inspection_year = $_post['sy']; $check_for_the_report = $db->single("select id dg_inspection_forms inspection_month = :sm , inspection_year = :sy ",array("sm"=>"$inspection_month","sy"=>"$inspection_year")); if($check_for_the_report){ echo "false"; } else { echo "true"; }
the form validation part:
$('.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 report selected month , year 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 record selected month , 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(); } }); } } });
you need separate php file process database you
ajax php file should this:
<?php if(isset($_post['month'])){ //connect database $link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db"); if (!$link) { echo "error: unable connect 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 * table_name month=".$_post['month']." , year=".$_post['year']); $row_count = mysqli_num_rows($result); } $arr = array(); $arr['row_count'] = $row_count; echo json_encode($arr); exit;
and jquery function:
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']; } }); }
Comments
Post a Comment