javascript - php: how to validate textbox using jquery ajax -
i have textbox, validate using jquery-ajax once user focusout textbox.
validation include:
server side, value goes php page , have validate it
if validation success,show alert("succes"); else if error alert("failed");
i have doubt how jquery ajax show alert("failure"); till , have done success.
i know below mention code,furthure should solve problem? please help
<script type="text/javascript"> $(function() { $('input[id^="datepicker"]').on('focusout',function() { $.ajax({ url: "ajax_frmdate_validation.php?txtval="+$(this).val(), success: function(data){ alert('successfully ...'); } }); }); }); </script> -------------- -------------- <input class="plain" name="frmdate" value="" size="25" id="datepicker" /> and server side php code is:
$txtval =$_request['txtval']; $fromtimestamp = strtotime( $txtval); //---------my logic code goes here ----------------------------- $sid=$_session['id']; $result12=mysql_query("select * budget creater_id = '$sid'"); $num_rows = mysql_num_rows($result12); if($num_rows>0)//check if empty or not { while($test12 = mysql_fetch_array($result12)) { $from_date = strtotime($test12['from_date']);//getting from-dates_column $to_date = strtotime($test12['to_date']);//getting to-dates_column if(isset($to_date) && isset($from_date)) { if((($fromtimestamp >= $from_date) && ($fromtimestamp <= $to_date)) || (($totimestamp >= $from_date) && ($totimestamp <= $to_date))) { $val = 'y'; //return y in meet condition } else { $val = 'n'; // return n if meet condition } } //---------------------result of logic code--------------------------------- if($val == 'y') //returns true { //validation fails - returns validation error break; } else if($val == 'n') { //validation true - returns validation error } } }
you can way:
$txtval = mysql_real_escape_string($_request['txtval']); $fromtimestamp = strtotime( $txtval); //---------my logic code goes here ----------------------------- $sid=$_session['id']; $result12=mysql_query("select * budget creater_id = '$sid'"); $num_rows = mysql_num_rows($result12); if($num_rows>0)//check if empty or not { while($test12 = mysql_fetch_array($result12)) { $from_date = strtotime($test12['from_date']);//getting from-dates_column $to_date = strtotime($test12['to_date']);//getting to-dates_column if(isset($to_date) && isset($from_date)) { if((($fromtimestamp >= $from_date) && ($fromtimestamp <= $to_date)) || (($totimestamp >= $from_date) && ($totimestamp <= $to_date))) { $val = 'y'; //return y in meet condition } else { $val = 'n'; // return n if meet condition } } //---------------------result of logic code--------------------------------- if($val == 'y') //returns true { echo "failed"; exit(); } else if($val == 'n') { echo "success"; exit(); } now in ajax call:
<script type="text/javascript"> $(function() { $('input[id^="datepicker"]').on('focusout',function() { $.ajax({ url: "ajax_frmdate_validation.php?txtval="+$(this).val(), success: function(data){ if (data == 'success') { alert("successfully validated"); } else { alert("failed"); return false; } } }); }); }); </script> rather alerting, better way show error message below textbox appending message if failed.
also proper escaping before checking using mysql_real_escape_string
update :
you need call ajax whenever date changes in input text field, instead of focusout can use onselect datepicker. when new date selected ajax call initiated.
so instead of calling earlier ajax on focusout can call on onselect of datepicker
$(function(){ $('#datepicker').datepicker({ onselect:function(date,instance){ $.ajax({ url: "ajax_frmdate_validation.php?txtval="+date, success: function(data){ if (data == 'success') { alert("successfully validated"); } else { alert("failed"); return false; } } }); } }); }); new update
try :
<script type="text/javascript"> $(document).ready(function() { $("#datepicker").datepicker({ dateformat: "yy-mm-dd" , onselect:function(date,instance){ $.ajax({ url: "ajax_frmdate_validation.php?txtval="+date, success: function(data){ if (data == 'success') { alert("successfully validated"); } else { alert("failed"); return false; } } }); } }); }); </script>
Comments
Post a Comment