开发者

Using AJAX to update information with every click

Ok I have a table that changes the displayed text once clicked on like the one displayed on http://www.w3schools.com/ajax/ajax_database.asp

I'm trying to update it based on each click. For example.

Page Load > 开发者_StackOverflow社区Load news article 1
onClick 1 > Load news article 2
onClick 2 > Load news article 3

All I want is for it to change based on each click, to a subsequent value. I have a php mysql database script that will pull the data from the database each time called.

The real question: Should I program the php to return a new table data cell with the new

oncLick="showNews($next_number)"

or should I leave that up to the AJAX, before it requests the information, just +1 it up.

I'm new to AJAX programming and not that experienced in PHP. I have searched everywhere and apologize if this is a redundant question. Just point me in the right direction.


write a php function to support to get the content by id. showNews(news ID). and then pass the newid with the ajax request. no need to change the newsid in the PHP.


I'm guessing something like this would be simplest:

var article = 0;
function showNews(){
   get_article(article);
   // magic
   article+=1;
}

To be honest, just pick the way which seems more natural to you. Unless this is only a small part of something huge, it won't matter.


if I understood right, you want to get the next news when clicking on your button...

I suggest you to use jQuery for Ajax request...

It could be like this:

<script type="text/javascript">
$(document).ready(function(){
     var result = 0;
     $('#myButton').click(function(){
       $.post('phpfunction.php',result,function(r){
             $(document).append(r);
       });
       result ++;
     });
});
</script>

And in PHP:

<?php
  $result_id = $_POST['result'];
  //SELECT * FROM WHERE id = $result_id;

?>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜