Hello.

I implement the possibility of notifications in the system, but I don’t understand how to get data from the ajax request and put it in different variables.

1) There is notification data

2) Data on the number of notifications

Upon receipt, they must be placed in different containers.

js code:

function notification(){ $.ajax({ url: "ajax_notification.php", cache: false, success: function(response){ if (response !== '') { $(".notification-content").html(response); soundClick(); } else { $(".notification-content").html('<div class="none-notification">У вас нет новых оповещений! </div>'); } if (count_lids !== '0') { $(".notification-icon").html('<div class="notification-count">'+ +'</div>'); } } }); } 

Php code

  $response = ''; $count_lids = ''; $request = mysql_query("SELECT `tel`, `name`, `id`, `email` FROM `mes` WHERE status_view = '1' and id_cabinet = '$myrow_cabinet[id]' LIMIT 0, 10"); $count_lids = mysql_num_rows($request); while($data = mysql_fetch_array($request)){ $search_id = iconv("windows-1251", "UTF-8",$data['id']); $search_name = iconv("windows-1251", "UTF-8",$data['name']); $search_email = iconv("windows-1251", "UTF-8",$data['email']); $search_phone = iconv("windows-1251", "UTF-8",$data['tel']); $response .= '<a href=details-'.$data['id'].' ><div class=search_name>'.$search_name.'</div></a><br>'; } echo json_encode($response); echo json_encode($count_lids); 

It works, but for some reason the data comes like this

http://joxi.ru/GrqM86bfN1b3rz

hike something I forgot or did wrong = / help ...

  • There is no data type, no request type. As you generally understood such ajax request. - Bastiane

1 answer 1

I imagine everything a little differently.

Exit php:

 exit(json_encode(array( 'notif' => $response, 'count' => $count_lids ))); 

Processing in js:

 function notification(){ $.ajax({ url: "ajax_notification.php", dataType: 'json', cache: false, success: function(response){ if (response.notif !== '') { $(".notification-content").html(response.notif); soundClick(); } else { $(".notification-content").html('<div class="none-notification">У вас нет новых оповещений! </div>'); } if (response.count !== '0') { $(".notification-icon").html('<div class="notification-count">'+ response.count +'</div>'); } } }); }