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
Preeti maurya
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
phoenix.mstu
source
to share
try it
var t = JSON.parse(json);
var max_use = t[0]["max_use"];
var used = t[0]["used"];
var amount = t[0]["amount"];
+4
Bushra shahid
source
to share
result is an array, you have a value at 0, so use below
result = [{"max_use":"0","period":"30","amount":"99"}][0]
select below
result.max_user
result.period
result.amount
or
result["max_user"]
result["period"]
result["amount"]
+1
Rahaman
source
to share