Auo从下拉列表中完成数据,但它存储id而不是名称

时间:2018-01-16 12:04:35

标签: php jquery ajax mysqli

我有两个下拉,第一个是客户端,第二个是srno。我必须选择客户端,并根据数据库srno来。多数民众赞成,但当我提交到数据库,而不是客户名称和序列号,它将存储m_id。代码在这里,

<div class="form-group">
          <label class="control-label col-lg-3" for="name">Client</label>
          <div class="col-lg-9"><input type="hidden" class="form-control" id="id" name="amc_id" required>  
         <select class="form-control select2" style="width: 100%;" name="client" id="sel_depart">
         <option value="">--- Select Client ---</option>
         <?php
                include('../dist/includes/dbcon.php');
                $queryc=mysqli_query($con,"select * from mach order by customer;")or die(mysqli_error());
                while($row=mysqli_fetch_array($queryc)){
                ?>
                  <option value="<?php echo $row['m_id'];?>"><?php echo $row['customer'];?></option>
                <?php }?>                  
                </select>

          </div>
        </div> 

        <div class="form-group">
          <label class="control-label col-lg-3" for="file">Sr.No</label>
          <div class="col-lg-9">
           <select class="form-control select2" style="width: 100%;" name="srno" id="sel_user" required readonly>

                </select>

          </div>
        </div>

这个是ajax脚本,

<script type="text/javascript">
        $(document).ready(function(){

            $("#sel_depart").change(function(){
                var deptid = $(this).val();

                $.ajax({
                    url: 'getUsers.php',
                    type: 'post',
                    data: {depart:deptid},
                    dataType: 'json',
                    success:function(response){

                        var len = response.length;

                        $("#sel_user").empty();
                        for( var i = 0; i<len; i++){
                            var id = response[i]['m_id'];
                            var name = response[i]['srno'];

                            $("#sel_user").append("<option value='"+id+"'>"+name+"</option>");

                        }
                    }
                });
            });

        });
    </script>

这个是带有srno脚本的getuser,

<?php
include('../dist/includes/dbcon.php');

$departid = $_POST['depart'];   // department id

$sql = "SELECT m_id,srno FROM mach WHERE m_id=".$departid;

$result = mysqli_query($con,$sql);

$users_arr = array();

while( $row = mysqli_fetch_array($result) ){
    $userid = $row['m_id'];
    $name = $row['srno'];

    $users_arr[] = array("m_id" => $userid, "srno" => $name);
}

// encoding array to json format
echo json_encode($users_arr);

所以我这样做会改变它会保存客户名称和srno。

1 个答案:

答案 0 :(得分:0)

在这里,我想您忘了收集您的客户名称....

     $("#sel_user").append("<option value='"+id+"'>"+name+"</option>");

您将值指定为m_id,因此在提交表单时会保存该值,因此请将值更改为客户名称

如果您在该表中有客户

,请先更改您的脚本
$sql = "SELECT m_id,srno,CUSTOMER_NAME_FIELD FROM mach WHERE m_id=".$departid;

while( $row = mysqli_fetch_array($result) ){
  $userid = $row['m_id'];
  $name = $row['srno'];
  $customer_name = $row['CUSTOMER_NAME_FIELD'];
  $users_arr[] = array("m_id" => $userid, "srno" => $name,"cust_name"=>$customer_name);
}

和javascript

   for( var i = 0; i<len; i++){
       var id = response[i]['m_id'];
       var name = response[i]['srno'];
       var cust_name = response[i]['CUSTOMER_NAME_FIELD'];
       $("#sel_user").append("<option value='"+name+"'>"+cust_name+"</option>");

       }