1

I am quite new to jQuery and AJAX. I am sending the request via AJAX to check file 'test' every 5 seconds which works fine. 'test' stands for test.php file.

<script>
$(document).ready(function () {
    function load() {
        var test = "<?php echo $test; ?>/";
        $.ajax({ //create an ajax request to load_page.php
            type: "GET",
            url: test,
            dataType: "html", //expect html to be returned    
            contentType: "text/html",            
            success: function (response) {
                $("#responsecontainer").html(response);
                setTimeout(load, 5000)
            }
        });
    }

    load(); //if you don't want the click
   // $("#display").click(load); //if you want to start the display on click
});
</script>

<div id="responsecontainer"></div>

test.php

            $id = $this->session->userdata('UserID');
        $get_friends_notification = $this->friends_model->get_friends_alert_by_ID($id);
        if(isset($get_friends_notification) && !empty($get_friends_notification))
        {
?>
            <div id="test" style="width:200px; height:auto; border:1px solid red;">
                <h3>Friend invitation from:</h3>
        <?php
                foreach($get_friends_notification as $key => $value)
                {
                    $new_id =   $get_friends_notification[$key] = $value["FrieInviter"];
                    $new_name = $get_friends_notification[$key] = $value["UserName"];

                    echo '<a href="'.base_url().'profile/get_user_data/'.$new_id.'">'.$new_name.'</a><br />';
                }
        ?>
            </div>
    <?php
        }

Then it just displays it in the div # responsecontainer which works fine too.

$("#responsecontainer").html(response);

In the file 'test' I am checking database if there were any updates. So I am pulling the information from DB and return to #responsecontainer. As it runs every 5 seconds, after it ran for the first time I would like to grab the last ID that I pulled and before it runs again and save it in variable and then I would like to pass that ID to the 'test' or process it differently. Basically I want to be able to use it. How can I do that??

EXAMPLE: ajax checks test.php file and find 5 rows. returns these 5 rows with 5 IDs. The last ID is number 5. In the meantime there were some other rows inserted so next time it will find more rows. Before it checkes again I want to tell it to not to check ID 1,2,3,4,5 but start from ID 6.

Also how does this method works with DB connections? Assuming that I have for example 500 users, and on all of theirs profiles that check would run every 5 seconds wouldnt it kill database connections?

zachu
  • 671
  • 2
  • 7
  • 19
  • What part of the PHP code is not working ? – Maximus2012 Apr 14 '15 at 21:39
  • Everything is working. I am just wondering how can I use the information I pull. For Example: ajax checks test.php file and find 5 rows. returns these 5 rows with 5 IDs. The last ID is number 5. Then before it checkes again I want to tell it to not to check ID 1,2,3,4,5 but start from 6. – zachu Apr 14 '15 at 21:39
  • I think that is a very generic question with answer specific to your application requirements. – Maximus2012 Apr 14 '15 at 21:43
  • When I am giving ajax the url: test.php can I pass a parameter (variable) to it? like in php you pass variables, parameters to functions for example: md5($test). ? – zachu Apr 14 '15 at 21:45
  • 1
    Yes. That you can do using either GET or POST. This is one example: http://stackoverflow.com/questions/4105211/jquery-ajax-post-to-php You should be able to find others similar to this. – Maximus2012 Apr 14 '15 at 21:46
  • Thank you I will have a look. Also is it possible to return the data from test.php to the file where ajax is called from? So ajax sends request to test.php and test.php returns $result ? – zachu Apr 14 '15 at 21:49
  • 1
    Yes. That is the whole point behind AJAX request/response process. success: function (response) { is where you will do that. – Maximus2012 Apr 14 '15 at 21:52
  • You probably should break this into two separate questions. Asking about `wouldnt it kill database connections` at the end of a lengthy, mostly unrelated question is likely to get lost, especially on those who might try to give database advice and give up reading after the first few paragraphs. – user2338816 Apr 14 '15 at 22:32

1 Answers1

0

Basically you need to add a pgination effect in here. Pass a parameter in the ajax request for example : if you are getting 5 records at a time, then initialize a variable say current_page = 0 , increment the same as you request via ajax

    var current_page=0;
function load() {
    var test = "<?php echo $test; ?>/";
    $.ajax({ //create an ajax request to load_page.php
        type: "GET",
        url: test,
        data : current_page
        dataType: "html", //expect html to be returned    
        contentType: "text/html",            
        success: function (response) {
            if(response.length!==0){
                $("#responsecontainer").html(response);
                current_page+=1;
            }
            setTimeout(load, 5000)
        }
    });
}

in the php page make the necessary changes (hope you know how pagination is done).

itssajan
  • 820
  • 8
  • 24