开发者

show news live with jquery and php

I have something like this:

<input id="num" type="text">

// var num = $("#num").val();
< ?php
$sql=mysql_query("SELECT * FROM news WHERE 
id= num //???
"); 

while($row=mysql_fetch_array($sql))
{
?>

< div id="main">
< ?php echo $row['title']; ?>
< ?php echo $row['body']; ?>
< /div>
< ?php
}

how do I write in input id num and view new news without refreshing the page? I know that I have to use jq开发者_如何学运维uery and ajax. i can do it in only jquery, but i cant combine this with PHP.

thanks!


have you considered using Json , Jquery , PHP together ? Call a page at some interval using jquery.This might give some pointers .

DisplayNews.php

 <input id="num"><input type="submit" onclick="loadNews()"/>
  <div id='content'></div>
<script>
     function loadNews(){
         $.getJSON("http://www.YourSite.com/GetNews.php?id=" + $("#num").val(), 
              function(data) {
                  $('#content').html('Latest News: ' + data.news + '<br/><br/>');
              }); 
         //setTimeout(loadNews(),5000); use this if you want to refresh ever 10seconds
      }
</script>

GetNews.php

< ?php
$sql=mysql_query("SELECT * FROM news WHERE id= num)
Execute query....
echo '{news:' . result . '}';
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜