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.

fmc
  • 490
  • 7
  • 24
  • What are you trying to click that is labelled `$('fetch')`? – Rasclatt Jan 21 '16 at 03: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 – roullie Jan 21 '16 at 04: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. – Keyur Mistry Jan 21 '16 at 04:01
  • Where is the `#names` id? This seems very incomplete as far as a code sample goes. – Rasclatt Jan 21 '16 at 04:01
  • @roullie - Thanks. I fixed the $('#fetch') – fmc Jan 21 '16 at 04:02
  • @KinjalMistry - I updated my post. – fmc Jan 21 '16 at 04:03
  • @Rasclatt - `#names` is a select box that holds client names. That's why I'm parsing it, to get the `clientID`. The `clientIDs` are set as the option values for the names. – fmc Jan 21 '16 at 04:05
  • I apologize for the shoddy post, folks. It's cleaned up now, and I still need help with the issue I posted. – fmc Jan 21 '16 at 04:20

1 Answers1

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

Vegeta
  • 1,319
  • 7
  • 17
  • I just plugged that in and got nothing. The select box stays on `SELECT` instead of changing to the value from the db. – fmc Jan 21 '16 at 04:42
  • 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. – fmc Jan 21 '16 at 04:47
  • I'm new to JQuery and really appreciate what you just taught me there. – fmc Jan 21 '16 at 04:47
  • I am asking in client side. replace your success function with success:function(re){ alert(re); } – Vegeta Jan 21 '16 at 04:49