How to autofill city name by giving pin code using javascript and php?

1.6k Views Asked by At

I am not able to autofill city by giving pincode even though database connection successfull. Below is the code: pin.php

<body>
<form>
 <label >Pincode </label>
 <input type="text" class="form-control"  placeholder="Enter Pin Code" name="pin_value" id="pin" maxlength="6"  pattern=".{6,6}" title="Exacty 6 digits"  onkeypress="return isNumberKey(event)"  onkeypress="set_city()" required="">
<label >City </label>
<input type="text" class="form-control"  placeholder="Enter City" name="city_value" id="city" onkeypress="return isCharacterKey(event)" >
<?php

      include("configs.php"); 
      $pin = $_POST['pin'];
      $query = $conn->query("SELECT cityname FROM pincodes WHERE pincode = '$pin'");
      while($row = $query->fetch_assoc()) {
      ?>
        <option> <?php echo $row["cityname"];?></option>
      <?php
      }?>
</form>
<script>
function set_city(){
    var pin = document.getElementById("pin").value;

    $.ajax({
        url: "load_pincode.php",
        method: "post",
        data: {pin: pin},
        success: function(response){
            if(response == ""){
                alert("please enter pincode");
            }
            else{
                $("#city").val(response);
            }
        }

    });
}

</script>
</body>

load_pincode.php

<?php
    @ob_start();
    session_start();
    include("configs.php");

    $pin = $_POST['pin'];

    if($_POST["pin"]) 
    {
        $uid = $_SESSION['user_id'];
        $user_type = $_SESSION["user_type"];
        if($user_type=="lite"){
            $cur_uid = $uid;

            $uid = $_SESSION["ad_id"];
        }
        $cur_bid = $_SESSION["default_business_id"];
        $q = $conn->query("SELECT user_pin FROM business WHERE year = '$curr_year' AND user_id = '$uid' AND business_id = '$cur_bid'");
        $c;
        while($r=$q->fetch_assoc()){
            $c = $r["user_pin"];
        }
        $qg = $conn->query("SELECT cityname FROM pincodes WHERE pincode = '$pin' ");
        while($rg = $qg->fetch_assoc()) {

            $p = $rg["cityname"];

        } 
    }else{
        echo "prob";
    }
?>

Its neither displaying error nor output.I am stuck in this auto-filling not able to move further. How will I get to know whether it is passing value of pin to load_pincode.php ?

2

There are 2 best solutions below

3
On

You need to return json array of the result and modify the ajax script to display the city. Please see below.

load_pincode.php

<?php
        @ob_start();
        session_start();
        include("configs.php");

        $pin = $_POST['pin'];

        if($_POST["pin"]) 
        {
            $uid = $_SESSION['user_id'];
            $user_type = $_SESSION["user_type"];
            if($user_type=="lite"){
                $cur_uid = $uid;

                $uid = $_SESSION["ad_id"];
            }
            $cur_bid = $_SESSION["default_business_id"];
            $q = $conn->query("SELECT user_pin FROM business WHERE year = '$curr_year' AND user_id = '$uid' AND business_id = '$cur_bid'");
            $c;
            while($r=$q->fetch_assoc()){
                $c = $r["user_pin"];
            }
            $qg = $conn->query("SELECT cityname FROM pincodes WHERE pincode = '$pin' ");
            while($rg = $qg->fetch_assoc()) {

                $p = $rg["cityname"];

            } 

            echo json_encode(array('city' => $p)); exit();
        }else{
            echo "prob";
        }
?>

-------------------------------------------------
$.ajax({
    url: "load_pincode.php",
    method: "post",
    data: {pin: pin},
    success: function(response){
        if(response == ""){
            alert("please enter pincode");
        }
        else{
            $("#city").val(response.city);
        }
    }

});
0
On

I am not going through your whole code but First of all you are getting the value of city name according to the pincode but not returning it ,

  $qg = $conn->query("SELECT cityname FROM pincodes WHERE pincode = '$pin' ");
      while($rg = $qg->fetch_assoc()) {

        $p = $rg["cityname"];

     } 
   echo json_encode($p); // here getting the city name