Following is my json response :
{"fname":"abc","lname":"xyz","email":"[email protected]","description":[{"city":"abcxyz","address":"AX","country":"US","date":"2020-02-01"},{"city":"abcxyz","address":"AX","country":"US","date":"2020-02 01"}], "city":"abcxyz","address":"DS","country":"US","Month":"12","Year":"2012"}
And following is my code :
success: function(data)
{
var userinfo=eval(data);
alert(userinfo['fname1']; //display correct result
alert(userinfo['description']['city']; //display undefined
if(!$("#fname1").val()) $("#fname1").val(userinfo['fname']);
if(!$("#lname1").val()) $("#lname1").val(userinfo['lname']);
}
In success function if I am trying to alert alert(userinfo['fname1'] then its showing me the correct result i.e. displaying name but if I am trying to alert alert(userinfo['description']['city'] then its showing me undefined in alert.
Then I tried using each loop but still its not working
$.each(userinfo['description'], function() {
alert(userinfo'description']['city']; //not working
$("#city option[value="+userinfo['description']['city']+"]").attr("selected", "selected");
});
Following is my html :
<select name="country" id="country">
<?php
foreach ($this->description as $country) {
?>
<option value="<?php echo $country['country']; ?>" <?php echo ($this->params['country'] == $country['country']) ? "selected='selected'" : ""; ?>><?php echo $country['country']; ?></option>
<?php }
?>
</select>
Can anyone telll me where I am going wrong. Thanks.
descriptiona list of objects withcity,address, etc; but you also have some of the same fields directly in the main object (and others are different). Whichcitydata matters to you, the main one or the ones in the list?