简体   繁体   English

根据php jQuery中的下拉选项单击按钮时填充文本框

[英]Fill textbox when click a button according to dropdown selection in php jQuery

My process is like this: I have a dropdown menu and text box. 我的过程是这样的:我有一个下拉菜单和文本框。 When I select an id (unique id) from dropdown and then click submit button want to display corresponding name to text box. 当我从下拉列表中选择一个id(唯一ID)然后单击“提交”按钮时,要在文本框中显示相应的名称。

My database fields : 我的数据库字段:

  1. id (Auto increment) id (自动增量)
  2. AgencyName_id (unique id) AgencyName_id (唯一ID)
  3. Name 名称

dispay.html dispay.html

<select name="agencyID_dwn" class="idLookup_dwn"  id="agencyID_dwn" >
<option selected>...Select...</option>
  <?php
  while($row = mysqli_fetch_array($result)){
  ?>

   <option value="<?php echo $row['AgencyName_id'];?>"> 
   <?php echo $row['AgencyName_id'];?></option>
 <?php
  }
   ?>
    </select>
        // for input text
        <input type="text" id="testid">
        // submit button
         <input type="submit" name="lookupSubmit">

dataGet.php dataGet.php

            <?php
            if (isset($_POST["lookupSubmit"])) {

            $user_id=$_POST['agencyID_dwn'];

             $query = "select * from AgencyHome where AgencyName_id = '$user_id'" ;
        $result=mysqli_query($db, $query);

        $data =  mysqli_fetch_assoc($result);

       echo json_encode($data);
         exit();

       }

        ?>

myjson.js myjson.js

           <script src="//code.jquery.com/jquery-1.11.2.min.js">                </script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js">  </script>
    <script type="text/javascript">
      $(document).ready(function(){

      $('#agencyID_dwn').change(function(){

    var reg_number = $(this).val();
      var data_String;
     data_String = 'reg_number='+reg_number;
    $.post('dataGet.php',data_String,function(data){
      var data= jQuery.parseJSON(data);

       $('#testid').val(data.Name);


          });
       });
      });

      </script>

When i click submit button I got the database results as array in "dataGet.php".But in textbox did not display the result.Any mistake in my code? 当我点击提交按钮时,我在“dataGet.php”中将数据库结果作为数组。但是在文本框中没有显示结果。我的代码中有任何错误?

try like this in dataGet.php, 在dataGet.php中尝试这样,

while ($row = mysqli_fetch_assoc($result)) {
    echo $row["Name"];
}

here is your answer 这是你的答案

your index.php 你的index.php

    <?php
    $conn = mysqli_connect("localhost","root","","test_db");

 ?> 

<!DOCTYPE>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="//code.jquery.com/jquery-1.11.2.min.js">                     </script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js">  </script>
 <script type="text/javascript">
  $(document).ready(function(){

  $('#agencyID_dwn').change(function(){

var reg_number = $(this).val();
  var data_String;
 data_String = 'reg_number='+reg_number;
$.post('dataGet.php',data_String,function(data){
    console.log(data);
  var data= jQuery.parseJSON(data);

   $('#testid').val(data.Name);


      });
   });
  });

  </script>

<body>
<form>
<select name="agencyID_dwn" class="idLookup_dwn"  id="agencyID_dwn" >
<option selected>...Select...</option>
 <?php
  $query = "select AgencyName_id from AgencyName";
 $result = mysqli_query($conn,$query);
 while($row = mysqli_fetch_array($result)){
  ?>

   <option value="<?php echo $row['AgencyName_id'];?>"> 
   <?php echo $row['AgencyName_id'];?></option>
  <?php
   }
   ?>
    </select>
    // for input text
    <input type="text" id="testid">
    // submit button
       <input type="submit" name="lookupSubmit">
  </form>
 </body>
 </html>

and your dataGet.php file as bellow 和你的dataGet.php文件如下

     <?php

   $conn = mysqli_connect("localhost","root","","test_db");

        $reg_number=$_POST['reg_number'];

   $query = "select * from AgencyName where AgencyName_id = '$reg_number'" ;
    $result=mysqli_query($conn, $query);

    $data =  mysqli_fetch_assoc($result);
   // print_r($data);
      echo json_encode($data);
     exit();


    ?>
     just ccheck your table name and all will work

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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