먼저
jQuery.ajax({
type: "GET",
url: "YOUR PHP FILE PATH HERE",
data: {username:'username'}, // send the data to the PHP page here like username or whatever
dataType: "json",
success: function(data)
{
var unread_emails=data.email;
var unread_comments=data.comments;
var unread_whatever=data.whatever;
jQuery('li#emails').val("New Emails "+unread_email);
jQuery('li#comments').val("New Comments "+unread_comments);
jQuery('li#whatever').val("New whatever "+unread_whatever);
},
error: function()
{
alert("Some error");
});
, 당신은 당신의 수를 포장 할 수 있습니다 span
또는 기타 태그를 id
또는 class
으로 수정하고 아약스 요청 후에 업데이트하십시오.
HTML
<ul>
<li>new emails (<span id='newEmails'>3</span>)</li>
<li>new comments (<span id='newComments'>12</span>)</li>
<li>new whatever (<span id='newWhatever'>3</span>)</li>
</ul>
JS는
이 스크립트는 yourpage.php
에 아약스 요청을하고 JSON 인코딩 된 데이터를 얻을. 그런 다음 JSON을 디코딩하고 HTML에서 카운트를 업데이트합니다.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$.ajax({
type: 'POST',
url: 'yourpage.php',
success: function(response){
//parse your json data
r = $.parseJSON(response);
//update your html
var newEmails = r.emailCount;
var newComments = r.commentCount;
var newWhatever = r.whatever;
$('#newEmails').html(newEmails);
$('#newComments').html(newComments);
$('#newWhatever').html(newWhatever);
},
error: function(xhr, status, errorThrown){
//handle ajax error
}
});
});
</script>
PHP
데이터베이스에서 사용자의 수를 모두 가져 오기 및 배열 안에 저장합니다. 마침내 배열을 인코딩하고 에코합니다.
<?php
//your database operation
$data = array();
$data['emailCount'] = 5; // your email count from database
$data['commentCount'] = 10; // your comments count from database
$data['whatever'] = 15; // your whatever from database
echo json_encode($data); //encode your data in JSON format
참고 : 당신은 당신이 혼란스러워하는 경우 JSON을 사용하지 않아도됩니다. 당신은 단지 PHP에서 필요한 데이터를 반향하고 응답으로 얻을 수 있습니다. 그러나 JSON을 사용하여 데이터를 쉽게 인코딩하고 디코딩 할 수 있습니다.
당신이 필요한 것은 http://api.jquery.com/jQuery.ajax/ – zzlalani
입니다. jquery를 살펴보기 전에 아마도 ajax 패러다임을 살펴 보는 것이 좋습니다. http://en.wikipedia.org/wiki/Ajax_ (프로그래밍) – Mangiucugna