Shakti Singh
Shakti Singh

Reputation: 86346

Background refreshing a page from another page PHP+Ajax

I want to refresh a php page from another php page. The refresh will be a background process. End user never know there is a refresh from this page to another page.

I mean to say I want to refresh a specific page ('www.example.com/somepage.php') when user enter in to the www.example.com/apage.php.

Let me be more specific to my question, Well, when we press F5 key from our keyboard the current web page get refreshed that it is a foreground process. If I have to do this from another web page as a background process where to start.

How could I do this? I am supposing this could be done via ajax.

Please give some advise.

Thanks.

Upvotes: 2

Views: 4033

Answers (2)

Alfred
Alfred

Reputation: 61771

AJAX

If I understand your question correct you want to use xmlhttprequest(AJAX). The easiest way to achieve this is by using jquery.

First you need to load jquery into your page using

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>

Next add the following code to your page:

<script type="text/javascript">
  $.get('somepage.php', function(data) {
    alert(data);
  });
</script>

Keep in mind that when using AJAX that some browser have same origin policy. But you can circumstance that if you only want to load the page by doing json-p. Like you saw above loading script from other websites(load jquery) is no problem at all.

Asynchronous PHP calls

Let me be more specific to my question, Well, when we press F5 key from our keyboard the current web page get refreshed that it is a foreground process. If I have to do this from another web page as a background process where to start.

I also think this stackoverflow topic which explains Asynchronous PHP calls might apply to you.

Upvotes: 3

cvolny
cvolny

Reputation: 405

I'm not really sure what you're asking. Are you asking how to change the behavior of someone navigating away from a page to load new data into the current page using javascript?

If that's the case, you would use the onUnload event and attach some javascript code that can load new content for you based on the link you're clicking.

jQuery is a really nice javascript library you can use to attach a handler to the unload event (http://api.jquery.com/unload/). It also has a pretty quick function called load that you can use to inject another page's content into a jquery object (http://api.jquery.com/load/).

For example:

$('div#output').load('www.site.com/apage.php');

This will request the page at www.site.come/apage.php and load its contents into the div with id=output.

Upvotes: 1

Related Questions