this is my php script
<?php
$wishes_array["wishes_text"] = null;
$wishes_array["total"] = null;
$count = 0;
// wishes has all records
if(!is_null($wishes))
{
foreach($wishes as $wish)
{
if($wish->text != null)
{
$wishes_array["wishes_text"][$count] = html_escape($wish->text);
$count++;
}
}
$wishes_array["total"] = sizeof($wishes);
echo json_encode($wishes_array);
}
?>
background
a user sends his wishes using a button, but he has an option to send some message with his wish, suppose person A just sent wish, but person B sent wish with text "happy birthday", person C sent wish with text "have a great birthday"
now using this array we have array which tells total
number of wishes recieved(with and without wish text) and wishes
the text of the wishes sent by person.
problem
unable to use parse the json using jquery, here is what i've done and it return undefined
var total_start = '<div style="padding: 3px;margin: 10px auto;"><i class="fa fa-heart" style="background: #e53935; color: #fff;padding: 5px;border-radius: 50%;"></i> ';
var total_end = '</div>';
var startString = '<div style="padding: 3px;margin: 10px auto;"><i class="fa fa-heart" style="background: #e53935; color: #fff;padding: 5px;border-radius: 50%;"></i>';
var endSting = '</div>';
var wishes = '';
var count = 0;
if(data["wishes_text"] != null){
$.each(data, function(index, element) {
wishes += startString;
wishes += ' ' + data["wishes_text"][count];
wishes += endSting;
count++;
});
}
var total = "";
if(data["total"] == 1)
{
total = total_start + data["total"] + ' person wished' + total_end;
}
else
{
total = total_start + data["total"] + ' people wished' + total_end;
}
$('#wish_div').html(total + wishes);
ajax response json
{"wishes_text":["wish 1","wish 2"],"total":3}