duanjing1023 2018-07-25 10:14
浏览 32

如何使用javascript和php给代码自动填充城市名称?

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条回答 默认 最新

  • dsfsdfsd34324 2018-07-25 10:22
    关注

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

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作