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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?