du656637962 2018-05-16 00: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 04: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部