-2

即使数据库连接成功,我也无法通过提供密码来自动填充城市。下面是代码: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";
    }
?>

它既不显示错误也不显示输出。我被困在这种自动填充中,无法进一步移动。我如何知道它是否将 pin 的值传递给 load_pincode.php ?

4

2 回答 2

0

您需要返回结果的 json 数组并修改 ajax 脚本以显示城市。请看下文。

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);
        }
    }

});
于 2018-07-25T10:22:29.233 回答
0

我没有查看您的整个代码,但首先您根据密码获取城市名称的值但不返回它,

  $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 
于 2018-07-25T10:22:46.087 回答