<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.3.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<div style="text-align:center;margin:0 auto;"><input type='text' name='serial_number' id='sn' maxlength='17' style="width: 400px;height: 60px;font-size: 30px;text-align: center;"></div>
</br>
<div style="text-align:center;margin:0 auto;"><input type='button' id='check_username_availability' value='Check Availability'></div>
<div id='username_availability_result' ></div>
<script type="text/javascript">
$(document).ready(function () {
$("#check_username_availability").click(function() { //user types username on inputfiled
$('#username_availability_result').html("44444444");
var username = $("#sn").val(); //get the string typed by user
$.post('check_detail.php', {'serialnumber':username}, function(data) { //make ajax call to check_username.php
$("#username_availability_result").html(data); //dump the data received from PHP page
});
});
});
</script>
Code (PHP)
<?php
$servername = "localhost";
$username = "root";
$password = "";
//check we have username post var
if(isset($_POST["serialnumber"]))
{
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
die();
}
//try connect to db
$connecDB = mysqli_connect($servername, $username, $password,'certificates')or die('could not connect to database');
//check username in db
$results = mysqli_query($connecDB,"SELECT SerialNumber FROM certificates WHERE SerialNumber ='1234567'");
//return total count
$username_exist = mysqli_num_rows($results); //total records
//if value is more than 0, username is not available
if($username_exist) {
echo 'Data Found';
}else{
echo 'Data Not Found';
}
//close db connection
mysqli_close($connecDB);
}
?>
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
die();
}