Reputation: 65
I want to accomplish the following 1. If the zip code is present in my database then print eligible for delivery 2. Else Not eligible for delivery
I wrote code as below: html
<form>
Pincode: <input autocomplete="off" type="text" maxlength="6" name="pincode" class="pincode" id="pin_code" placeholder="Enter Pincode" onkeypress='validate(event)'/><br>
<span class="check" ></span> <br/><br/>
javascript and ajax
<script type="text/javascript">
$(function()
{
$('.pincode').keyup(function()
{
var checkcode=$(this).val();
if(checkcode){
$('.check').show();
$('.check').fadeIn(400).html('<img src="image/ajax-loading.gif" /> ');
var Str="pin=" + checkcode;
$.ajax({
type: "POST",
url: "available.php",
data: Str,
cache: false,
success: function(result){
if(result==''){
$('.check').html('<img src="image/error.png" /> This pincode is not valid');
$(".check").removeClass("red");
$('.check').addClass("green");
$(".pincode").removeClass("yellow");
$(".pincode").addClass("white");
}else{
$('.check').html('<img src="image/accept.png" /> This pincode is valid');
$(".check").removeClass("green");
$('.check').addClass("red")
$(".pincode").removeClass("white");
$(".pincode").addClass("yellow");
}
}
});
}else{
$('.check').html('');
}
});
});
</script>
php code for available.php
<?php
$mysql_db_hostname = "localhost";
$mysql_db_user = "healthmate";
$mysql_db_password = "healthmate";
$mysql_db_database = "test";
$con = mysql_connect($mysql_db_hostname, $mysql_db_user, $mysql_db_password) or die("Could not connect database");
mysql_select_db($mysql_db_database, $con) or die("Could not select database");
if(isset($_POST['pincode']) && !empty($_POST['pincode'])){
$query="select * from cod_locations where pin=$pincode";
$res=mysql_query($query);
$count=mysql_num_rows($res);
$HTML='';
if($count > 0){
$HTML='delivery available';
}else{
$HTML='not there';
}
echo $HTML;
}
?>
Problem : Even the valid code in the database is shown as invalid. Any help will be appreciated. I guess the problem lies with result in ajax part.
Upvotes: 1
Views: 2577
Reputation: 91734
There are a few problems here:
var Str="pin=" + checkcode;
. So on the php side you would need $_POST['pin']
;$HTML
variable), so the result
variable in your success function will never be empty;Upvotes: 1