on form submit i am making an AJAX request to PHP code, in response this is what i get.
var data = {
"empty":{
"game_sais_no":"Season cannot contain empty value",
"game_sc_no":"Category cannot contain empty value",
"game_st_no2":"Visiting team connot contain empty value",
"game_room_no_2":"Visiting room cannot contain empty value",
"game_room_no_1":"Local chamber cannot contain empty value",
"game_date":"Game date should be specified",
"game_time":"Game time should be specified",
"game_time_start":"Game start time should be specified",
"game_time_close":"Game close time should be specified",
"game_place_no":"Arena \/ Lot should be specified",
"game_status":"Game status should be specified"
}
}
1. i want to access single value. i tried accessing it like this.
data.empty.game_sais_no it returns me the value of undefined.
2. i want to loop through the json object and display all the messages back to the user. i tried using
$.each(data, function(index, value)(){
//build the markup.
});
this is giving me unexpected result. where am i going wrong?
UPDATE :
i am not sure but for some reason it is giving me weird result, let me show you what exactly i am doing.
here is my ajax call to php.
$('#gce_game_btn').on('click', function(){
var formData = $('#gce_game_form').serialize();
$.ajax({
type : 'POST',
url : 'accueil.php?m=ajax&game=1',
data : formData,
success : function(data) {
//
}
});
});
here is the array which i am trying to send back.
Array
(
[empty] => Array
(
[game_sais_no] => Season cannot contain empty value
[game_sc_no] => Category cannot contain empty value
[game_st_no2] => Visiting team connot contain empty value
[game_room_no_2] => Visiting room cannot contain empty value
[game_room_no_1] => Local chamber cannot contain empty value
[game_date] => Game date should be specified
[game_time] => Game time should be specified
[game_time_start] => Game start time should be specified
[game_time_close] => Game close time should be specified
[game_place_no] => Arena / Lot should be specified
[game_status] => Game status should be specified
)
)
i am using json_encode() and echoing it back. which in turn gives me this as string.
{
"empty":{
"game_sais_no":"Season cannot contain empty value",
"game_sc_no":"Category cannot contain empty value",
"game_st_no2":"Visiting team connot contain empty value",
"game_room_no_2":"Visiting room cannot contain empty value",
"game_room_no_1":"Local chamber cannot contain empty value",
"game_date":"Game date should be specified",
"game_time":"Game time should be specified",
"game_time_start":"Game start time should be specified",
"game_time_close":"Game close time should be specified",
"game_place_no":"Arena \/ Lot should be specified",
"game_status":"Game status should be specified"
}
}
It works fine. Check the demo.