du656637962 2018-05-16 08:45
浏览 57
已采纳

Ajax模式:将选定的值返回到下拉列表到更新表单

I have been searching everywhere on this website and tried many solutions but none worked for me ! My problem is the following :

I have 2 tables :

TABLE INFO_STATUS (status_id, status_desc)
and
TABLE INFO_MARQUES (comp_id, comp_desc, comp_status(INT))

I am linking comp_status and status_id so that the row in INFO_MARQUES will get the current status.

What I would like to do? (EDITED FROM HERE)
2. get the current status value of info_marques located in comp_status automatically selected in the dropdown list when I open the update form

Example I want to update this company :

comp_id : 15
comp_desc : WEISS
comp_status : 1

Assuming that in table info_status :

status_id = 1
status_desc = FOLLOW-UP

When I open the update form I want to see this : Check this

My Modal : (WORKING)

                        <select class="form-control" name="select_status">
                        <option value=''>Selectionner</option>
                    <?php 
try {
                    $user = new USER();
                    $output = array();
                    $stmt = $user->runQuery("SELECT * FROM info_status");
                    $stmt->execute();
                    $result=$stmt->fetchAll();

                    foreach($result as $row) {
                        echo '<option value='.$row["status_id"].'>'.$row["status_desc"].'</option>';
                    }
}
                    catch(PDOException $e) {
                    printf('Erreur MySQL %d : %s', $e->getCode(), $e->errorInfo[2]);
                    exit;

}
?>
                    </select>

My Ajax

$(document).on('click', '.update', function(){ 
        var user_id = $(this).attr("id");
        var array = [];
        $('select :selected').each(function(i,value)
        {
        array[i] = $(this).val();
        });     
        $.ajax({
            url:"partials/test/fetch_single.php",
            method:"POST",
            data:{user_id:user_id},
            dataType:"json",
            success:function(data)
            {
                $('#userModal').modal('show');
                $('#comp_name').val(data.comp_name);
                $('#comp_parent').val(data.comp_parent);

                $.each($('.select_status option'),function(a,b){

                if($(this).val()== data.comp_status){   
                $(this).attr('selected',true)
                }

                });

                $('.modal-title').text("Modifier un fichier client");
                $('#user_id').val(user_id);
                $('#user_uploaded_image').html(data.user_image);
                $('#action').val("Edit");
                $('#operation').val("Edit");

            }

        })
    });

then my PHP page : (WORKING)

if(isset($_POST["user_id"]))
{
    $output = array();
    $statement = $connection->prepare(
        "SELECT * 
FROM info_marques AS m 
LEFT JOIN info_status AS s 
ON s.status_id = m.comp_status 
WHERE m.id = '".$_POST["user_id"]."' 
LIMIT 1"
    );
    $statement->execute();
    $result = $statement->fetchAll();
    foreach($result as $row)
    {
        $output["comp_name"] = $row["comp_name"];
        $output["comp_parent"] = $row["comp_parent"];
        $output["comp_status"] = $row["status_desc"];

    }

    echo json_encode($output);
}

What am I missing here?

Cheers

  • 写回答

2条回答 默认 最新

  • dqstti8945 2018-05-16 12:05
    关注

    I am not sure that if you want to populate drop down or else select data based on your response, But if you want to select the data and you have options already in drop down, you can do this:

     $.each($('#select_status option'),function(a,b){
    
        if($(this).val() == data.comp_status){
                  $(this).attr('selected',true)
        }
    
        });
    

    But if you want to populate, you will have to append it using .append(). Do let me know if issue is something different and I may give you a solution.

    As you are saying you need all the status and then show the current status. what i would suggest is use .done() method of ajax. It will come handy, like:

    $("#select_status").prop('disabled',true)
    $('#select_id').append('<option>Please wait... Fetching status</option>');
    var status_options;
    $.ajax({
     url:"your_page_for_all_status.php",
     success: function(e){
        //add <option> tags to a variable using loop
     }
    
    }).done(function(){
     $.ajax({
       //ajax call to get current data. Compare and select data here. write your $.each here
      }).done(function(){
         $("#select_status").prop('disabled',false);
      });
    });
    

    You should be good to go now!!! I am sorry if I made it complex.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)