0

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

My 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:

$('#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:

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();
}

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.

8
  • What are you trying to click that is labelled $('fetch')? Commented Jan 21, 2016 at 3:59
  • 1
    $('fetch') is this a tag? maybe you need to add a # if it's an ID or a . if it's a class Commented Jan 21, 2016 at 4:00
  • What you exactly want to do? because firstly you are processing to click event of some tag name which is not there and you are passing data in ajax from some name field and processing the data in php file and then after you are trying to do action on the field which is not in the form element. e.g. you have select dropdown of id answer9 and in javascript code you are working on answer1. And also let me know the result you get in the ajax response. Commented Jan 21, 2016 at 4:01
  • Where is the #names id? This seems very incomplete as far as a code sample goes. Commented Jan 21, 2016 at 4:01
  • @roullie - Thanks. I fixed the $('#fetch') Commented Jan 21, 2016 at 4:02

1 Answer 1

1

try this:

replace with this:

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

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

Sign up to request clarification or add additional context in comments.

4 Comments

I just plugged that in and got nothing. The select box stays on SELECT instead of changing to the value from the db.
I got it. In my post, in the PHP & MySQL, I copied that from another page that was joining a few tables together. In my post here, I have: WHERE a.memberid = :memberid. I removed the a. from memberid and it works great. I think I need a break. Been at it for almost 17 hrs! Thank you, Vegeta. I appreciate you.
I'm new to JQuery and really appreciate what you just taught me there.
I am asking in client side. replace your success function with success:function(re){ alert(re); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.