Put json value from ajax into one or more text boxes
$('select[name=\'service_id\']').bind('change', function() {
service_id = $('select[name=\'service_id\']').val();
$.ajax({
url: 'myajaxfile.php&service_id=' + service_id,
dataType : 'json',
success: function(json) {
var max_use = json.max_use;
var used = json.used;
var amount = json.amount;
$('input[name=\'max_use\']').val(max_use);
$('input[name=\'used\']').val(amount);
}
});
});
Above is my piece of code. All I want is to bind a value from the json result that matches two or more text fields that don't happen. The json result is similar [{"max_use":"0","period":"30","amount":"99"}]
, which is very correct. When you issue an alert, it says undefined. It would be very helpful if I point out what the problem is? I tried searching on stackoverflow but couldn't find a perfect solution that works. Thank.
+3
source to share
3 answers
Your json is an array with an object in it. So you have to either change the json or access the object properties like this:
var max_use = json[0].max_use;
var used = json[0].used;
var amount = json[0].amount;
the correct json in your case will look like this:
{"max_use":"0","period":"30","amount":"99"}
without []
+3
source to share