将新行添加到表时,使用AJAX更新页面

I am in the process of learning AJAX, as I need it for a current project. I have a feed which I want to update live when a new row is added to a MYSQL table. I've actually done this with node.js, but unfortunately my clients server can't use NODE, so I am trying to see if it's possible with AJAX.

Thank you for your help!

You'll need to have the client either contact the server constantly via polling, or use a push technique such as comet or websockets. Otherwise your client has no way to know when new data has arrived.

Comet and websockets allow for the most real-time feel to the process since they actually send the data to the user client (your connected browser) as soon as it is available whereas with polling it's just your browser querying the server on an interval.

Disadvantage is that push techniques and certainly comet which requires a HTTP Keep Alive can be quite heavy on your server, especially for process based servers such as apache, their maximum connections get filled quickly and then held up until disconnect. There are tuning options and modern settings for it but it's not optimal: the problem even has an official name c10k and is one of the reasons you see event based servers gain popularity these days.

There isn't any way that i know about where MYSQL or PHP can push updates to a page when it is updated.

The best option would probably be running timer with javascript and check the database periodically to see if there have been any updates,

var refreshId = setInterval(function() { checkforUpdates(lastIndex) }, 10000);

Pass in the last row of your table index to the javascript function and the function makes an ajax call to a PHP that queries the database for any new rows, if any updates returned, add them to the table with javascript.

If you are also looking to check for updated rows, that gets a bit trickier!

its easier to do ajax stuff with jquery

$("#update_charges").live("click",function() {
     $.ajax(    
         {      
            type: "POST",
            url: "phpfile_toadd_updaterecord.php",
            data: "passvalue="+$('input_field_name').val(),
            success: function(msg)
           {
                            $('#result_div').html(msg); //msg contains any test echoed  from  //phpfile_toadd_updaterecord.php file
           }    
     });//end $.ajax    
 }

below can be your html code

<form>
<div id="result_div"></div>
<input type="button" name="update_charges" id="update_charges" />
<input type="hidden" name="input_field_name" id="input_field_name" value="anyvalue_to be passed to php file"/>
</form>

Jquery ajax help page