填写文本框,根据PHP的jQuery下拉选择单击按钮时(Fill textbox when clic

2019-10-22 08:00发布

我的过程是这样的:我有一个下拉菜单和文本框。 当我从下拉列表中选择一个ID(唯一ID),然后点击提交按钮要显示相应的名称文本框。

我的数据库字段:

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

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

            <?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

           <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>

当我点击提交按钮,我得到的数据库结果在文本框没有显示在我的代码result.Any错误数组中的“dataGet.php”。不过?

Answer 1:

这里是你的答案

您的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>

和你的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


Answer 2:

尝试像这样dataGet.php,

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


文章来源: Fill textbox when click a button according to dropdown selection in php jQuery