Reputation: 9
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');
}
Upvotes: 0
Views: 90
Reputation: 66389
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)
Upvotes: 2