0

I am new to json n php..
using code below to fetch data for radio button check box and text box with json . It is working fine but I donno how to populate data in select box in the same way .

function get_input_value(name, rval) {

    //console.log(name + ' && ' + rval);
    var i = 0;
    var chk = $('input[name="' + name + '"]');
    while (i < chk.length) {
        //console.log($(chk[i]));
        if ($(chk[i]).val() == rval) {
            //console.log('loopvalue => ' + i);
            $(chk[i]).prop('checked', true);
            }
            i++;
        }
}


function loadJson (table, id) {

    $.get("json-object.php", {'table': table, 'id':id}, function (data) {


        console.log(data);

        $.each(data, function (k, v) {
            if ($('input[name="'+k+'"]').is('input[type="text"]')) {
                $('input[name="'+k+'"]').val(v);
            } 
            if ($('select[name="'+k+'"]').is('input[type="radio')) {
                get_input_value(k,v);

            } 
            if ($('input[name="'+k+'"]').is('input[type="checkbox"]')) {
                get_input_value(k,v);
            } 
         console.log(k+' ==> '+v);  
        });
    }, 'json');
}
1
  • 1
    If I were a really bad boy, I'd go on your site to see what happens if I GET json-object.php?table=users&id=1. Commented Jun 18, 2012 at 9:36

1 Answer 1

2

Your code is just setting value of text boxes and auto select certain checkboxes based on the PHP output.

To have it auto set the proper item in drop down lists as well you need to change this wrong code:

if ($('select[name="'+k+'"]').is('input[type="radio')) {
    get_input_value(k,v);
} 

To this:

$('select[name="'+k+'"]').val(v);

(No point to check type of <select> since it got none)

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

Comments

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.