如何使用php mysql jquery ajax更新选择框 [英] How to update select boxes with php mysql jquery ajax

查看:139
本文介绍了如何使用php mysql jquery ajax更新选择框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像标题中所说的那样,我正在尝试更新选择框.但这对我不起作用.

Like the title says, I'm trying to update select boxes. But it isn't working for me.

我的HTML:

<div class="col-sm-3">
    <select class="form-control" id="answer9">
        <option value="00">Select</option>
        <option value="0">0 - No Problems</option>
        <option value="1">1 - Some Problems</option>
        <option value="2">2 - Considerable Problems</option>
        <option value="3">3 - Severe Problems</option>
    </select>
</div>

jQuery& AJAX:

JQuery & AJAX:

$('#fetch').click(function(){
    var nameid = parseInt($('#names').val());
    if(nameid == 0){
        $('#showerr').html("Please Select an Client").show().delay(3000).fadeOut("slow");
        $('#names').focus();
        return false;
    }
    $.ajax({
            url         : 'wsevaluationresult.php',
            type        : "POST",
            datatype    : "JSON",
            data        : {
                            'editvalues'    : 1,
                            'id'            : nameid
            },
            success:function(re){
                $('#answer9').val(show.answ9);
            }
    });
});

PHP& MySQL:

PHP & MySQL:

if(isset($_POST['editvalues'])) {
    $stmt = $db->prepare('SELECT clientid, answ9
                            FROM evals 
                            WHERE a.memberid = :memberid 
                            AND clientid = :id');
    $stmt->bindValue(':memberid', $_SESSION["memberid"], PDO::PARAM_INT);
    $stmt->bindValue(':id', $_POST['id'], PDO::PARAM_INT);
    $stmt->execute();
    $result = $stmt->fetchAll();
        foreach($result as $row) {
        header("Content-type: text/x-json");
        echo json_encode($row);
        }
    exit();
}

我在这里找到了一些建议,但没有一个真正解决这个问题.我想要的只是选择框,以反映来自answ9的db中的值.使用文本框时,ajax的工作方式对我有效.但是选择框是另一种完整的游戏.任何帮助表示赞赏.谢谢.

I've found a few suggestions here on SO, but none really address this. All I'm wanting is for the select box to reflect the value from the db for answ9. The way I'm doing the ajax works for me when I'm using text boxes. But the select boxes are a whole other game. Any help is appreciated. Thanks.

推荐答案

尝试一下:

替换为:

success:function(re){
    $('#answer9 option[value="'+re.answ9+'"]').attr('selected', 'selected');
}

在成功函数中,您将得到"re"之类的参数,然后答案必须是"re.answ9"之类的东西.如果您的回答正确

In your success function you are getting argument like "re" then answer must be something like "re.answ9". If your response is correct

这篇关于如何使用php mysql jquery ajax更新选择框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆