dongnao6858 2017-10-14 05:15
浏览 70
已采纳

在成功的ajax上,使用预选的选项值从另一个表创建一个下拉列表选择

For an ‘Edit’ modal, I initiate an ajax call to the php script named getSelectedMember.php that bring the information from the table items (Table 2). tabels

<?php 
   require_once 'db_connect.php';  

   $memberId = $_POST['member_id']; 

   $sql = "SELECT * FROM items WHERE itemID = $memberId";
   $query = $connect->query($sql);
   $result = $query->fetch_assoc();

   echo json_encode($result);
?>

This formulation_fk is in the table items a value of a select option from another table named formulation (Table 1).

This is code of 'edit.php' :

<form action=" " method="POST">
   <div>
      <label>Name</label>
      <input type="text"><br>
   </div>
   <div>
      <label>Formulation</label>
      <select id="editFormulation">
      </select>
   </div>
   <button type = "submit">Save changes</button>
</form>

My question is while updating a single item, how can I pass the select options from the formulation table in my edit form where the select option value will be the formulation_fk from the table items?

And this is my ajax call:

$.ajax({
  url: 'getSelectedMember.php',
  type: 'post',
  data: {
    member_id: itemID
  },
  dataType: 'json',
  success: function(response) {
    $("#editName").val(response.name);
    $("#editFormulation").val(response.formulation_fk);
    $(".editMemberModal").append( ? ? ? ? ? ? )
  }
});

For clarification of my question, let's think that to edit Water, the action flow would be like this:

  1. Click the edit button for ‘Water’.
  2. Ajax call to getSelectedMember.php to get the name (Water) and formulation_fk (1).
  3. On response, Name field will output ‘Water’ and Formulation filed will output a dropdown select from formulation table where option value = “1”

Something like this image below. Result

I have been trying to solve it for a while but I'll really appreciate any suggestion or expert help. Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dqq46733 2017-10-14 06:00
    关注

    The PHP code needs to return an array of all the formulations, in addition to the formulation_fk from the items table.

    $memberId = $_POST['member_id']; 
    
    $sql = "SELECT * FROM items WHERE itemID = $memberId";
    $query = $connect->query($sql);
    $result = $query->fetch_assoc();
    
    $sql = "SELECT * FROM formulation";
    $query = $connect->query($sql);
    $formulations = array();
    while ($row = $query->fetch_assoc()) {
        $formulations[] = $row;
    }
    $result['formulations'] = $formulations;
    
    echo json_encode($result);
    

    Then the AJAX code can fill in the <select> before setting its value.

    success: function(response) {
        $("#editFormulation").empty(); // Clear out previous value
        $.each(response.formulations, function() {
            $("#editFormulation").append($("<option>", {
                value: this.formulationID,
                text: this.formulation_name
            }));
        });
        $("#editFormulation").val(response.valuation_fk);
        $("#editName").val(response.name);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示