简体   繁体   中英

Zip Code Validation

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.

There are a few problems here:

  1. You are setting this to be sent to your php script: var Str="pin=" + checkcode; . So on the php side you would need $_POST['pin'] ;
  2. You are always echoing something out when a pincode is set (your $HTML variable), so the result variable in your success function will never be empty;
  3. You have an sql injection problem. You should switch to PDO or mysqli and prepared statements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM