动态添加选项值

时间:2018-06-28 19:28:02

标签: php mysql

我可以选择MySQL选项值。我的问题是,如果表中的值大于1,则总是打印一个选项。如果一名员工拥有多于1家公司,如何动态添加更多的期权价值?

例如:modal

它有2家公司,我想打印他拥有的所有公司,而不仅仅是1家。我该怎么做?

我在fetch.php中打印当前的MySQL数据。

index.php

    <label>Company:</label>
       <select name="company" id="company" class="form-control">
          <?php 
             $query2 = "SELECT * FROM company GROUP BY company_id";  
             $result2 = mysqli_query($connect, $query2);  
             while($row2= mysqli_fetch_array($result2)){
             ?>
             <option value="<?php echo $row2['company_id'];?>"><?php echo $row2['company_name'];?></option>
             <?php
             }
             ?>
       </select>

    <script>
       $(document).on('click', '.edit_data', function(){  
               var employee_id = $(this).attr("id");  
               $.ajax({  
                    url:"fetch.php",  
                    method:"POST",  
                    data:{'employee_id':employee_id},  
                    dataType:"json",  
                    success:function(data){  
                         $('#company').val(data.company);
                    }  
               });  
          });  

     </script>

fetch.php

if(isset($_POST["employee_id"]))  
 {  
        $query = "SELECT employee.id AS id, employee.name AS name, employee.address AS address, company.company_id AS company
            FROM employee
            LEFT JOIN employee_company ON employee.id = employee_company.employee_id
            LEFT JOIN company ON company.company_id = employee_company.company_id
            WHERE employee.id = '".$_POST["employee_id"]."'";  
      $result = mysqli_query($connect, $query);  
      $row = mysqli_fetch_array($result);  
      echo json_encode($row);  
 }

1 个答案:

答案 0 :(得分:1)

首先,您不能从下拉菜单中选择多个选项。您需要使用多项选择。

  <select name="company" id="company" class="form-control" multiple>
      <?php 
         $query2 = "SELECT * FROM company GROUP BY company_id";  
         $result2 = mysqli_query($connect, $query2);  
         while($row2= mysqli_fetch_array($result2)){
         ?>
         <option value="<?php echo $row2['company_id'];?>"><?php echo $row2['company_name'];?></option>
         <?php
         }
         ?>
   </select>

然后,您需要更改fetch.php,以返回所有公司ID的数组:

if(isset($_POST["employee_id"]))  
{  
    $query = "SELECT employee.id AS id, employee.name AS name, employee.address AS address, company.company_id AS company
        FROM szerepek 
        LEFT JOIN employee_company ON employee.id = employee_company.employee_id
        LEFT JOIN company ON company.company_id = employee_company.company_id
        WHERE employee.id = '".$_POST["employee_id"]."'";  
    $result = mysqli_query($connect, $query);
    if (mysqli_num_rows($result) == 0) {
        echo json_encode(null);
    } else {
        $companies = array();
        while ($row = mysqli_fetch_assoc($result)) {
            $companies[] = $row['company'];
        }
        $row['companies'] = $companies;
        echo json_encode($row);
    }
}

并更改Javascript以遍历它们并选择所有它们。

  $(document).on('click', '.edit_data', function(){  
       var employee_id = $(this).attr("id");  
       $.ajax({  
            url:"fetch.php",  
            method:"POST",  
            data:{'employee_id':employee_id},  
            dataType:"json",  
            success:function(data){  
                if (data) {
                    $("#company").val(data.companies);
                }
            }  
       });  
  });