<!DOCTYPE html>
<html>
<head>
<title>:: Validation with Ajax ::</title>
</head>
<body>
<h1>Validation with Ajax</h1>
<hr/>
<form action="<?php echo basename($_SERVER['PHP_SELF']); ?>" method="post">
<p>
<input type="text" id="mobile"> <span id="mobile-error"></span>
</p>
</form>
</body>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
var base_url = "http://localhost:8000/php-class/machine-task/validation-with-ajax/";
$(document).ready(function(){
$("#mobile").on("keyup",function(){
var mobile_no = $("#mobile").val();
$.ajax({
url:base_url+"ajax-validation/validate-mobile.php",
type:"POST",
data:{
mobileno:mobile_no,
},
success:function(response){
// var json_str = JSON.stringify(response.data);
$("#mobile-error").html(response);
}
});
});
});
</script>
</html>
<?phpheader("Content-Type:application/json");
$mobileno = trim($_POST['mobileno']);
if(empty($mobileno)):
echo json_encode(array("data"=>"<span Style='color:red;'>*Mobile No Cannot be Empty</span>"));
else:
echo json_encode(array("<span Style='color:green;'>Mobile is Not Empty</span>"));
endif;
<?php
$mobileno = trim($_POST['mobileno']);
if(empty($mobileno)):
echo "<span Style='color:red;'>*Mobile No Cannot be Empty</span>";
else:
echo "<span Style='color:green;'>Mobile is Not Empty</span>";
endif;
0 Comments