Solved

Dynamically Loading External PHP into a DIV using Prototype or scriptaculous

Posted on 2009-04-10
8
2,400 Views
Last Modified: 2013-12-12
I'm sure this is an easy question to answer for the right person.

What I want to do is use prototype and scriptaculous to dynamically load an external PHP file into a div.

To be as percise as possible:

I have an index.php file that has a php include contained inside a div.. something like:

<div id="container" style="width="300px;"

<? include:videos_1_2_and_3.php>

</div>

So this php file would get the 3 most recent videos from the MYSQL database. (Don't worry about this I already have it coded)

Now I also have another php file called
"videos_4_5_and_6.php"

What I want to do is create a link that says "more videos" or "next page" and when it is clicked on it loads videos_4_5_and_6.php where videos_1_2_and_3.php is (IE in the "container" div).

HOWEVER... THIS IS IMPORTANT!!!!!

It must load dynamically... So if I wanted to have 10 pages each containing 3 videos, it should only load those when the more videos button is clicked.

To be clear...  I know how to do this by hiding divs or using tabs... but I need the content to load dynamically.  I can't have 10 pages of 3 videos (or 30 videos) loading at the same time it will slow performance.

THE QUESTION:
How would I code this.  So that when a link is clicked on:
like <a href="javascript: load:videos_4_5_and_6.php inside div:container>MORE VIDEOS</a> would work??

Also I must be able to use PHP files because they are the ones calling on the database.

Thanks so much for the input.
0
Comment
Question by:ryanjameswebb
  • 4
  • 3
8 Comments
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
Comment Utility
There may be a strategic question at the root of this issue, and that goes to the difference between server-side and client-side scripting.  PHP is on the server side, JS is on the client side.  

When you have a construct like this:

<div id="container" style="width:300px;"
<? include:videos_1_2_and_3.php>
</div>

The PHP will get executed on the server at page load time and whatever it generates will appear inside the "container" DIV.  So if you use JS to go into the DOM and change the contents of the "container" DIV to something new, all that will happen is that the new information will be placed in the DIV on the client end of things.  It will not be parsed on the server as PHP, because there has not been another page load.

To make what you want to happen, you need to have a back-end PHP script that generates HTML (and JS - any client side stuff you want, including CSS, etc.).  This back-end script creates the contents you want to put into the "container" DIV and sends it to a listener on the client machine.  The listener puts the new information into the "container" DIV, and the updated page appears in the browser window.

I cannot give you a scriptaculous or prototype example, but I can give you a very simple teaching script that shows how AJAX works.  It is signed "Rasmus" which makes me think it may be from "Lerdorf" of PHP fame.  Can't remember where I got it, but it is elegant in its simplicity and shows the foundation of AJAX in a way that makes the client/server relationship easy to understand.  It's not "light reading" but if you give yourself the time to work through it carefully you will be wa-a-ay ahead on this project.

Hope this helps, ~Ray
I find a lot of this AJAX stuff a bit of a hype.  Lots of people have

been using similar things long before it became "AJAX".  And it really

isn't as complicated as a lot of people make it out to be.  Here is a

simple example from one of my apps.  
 

First the Javascript:
 

function createRequestObject() {

    var ro;

    var browser = navigator.appName;

    if(browser == "Microsoft Internet Explorer"){

        ro = new ActiveXObject("Microsoft.XMLHTTP");

    }else{

        ro = new XMLHttpRequest();

    }

    return ro;

}
 

var http = createRequestObject();
 

function sndReq(action) {

    http.open('get', 'rpc.php?action='+action);

    http.onreadystatechange = handleResponse;

    http.send(null);

}
 

function handleResponse() {

    if(http.readyState == 4){

        var response = http.responseText;

        var update = new Array();
 

        if(response.indexOf('|' != -1)) {

            update = response.split('|');

            document.getElementById(update[0]).innerHTML = update[1];

        }

    }

}
 

This creates a request object along with a send request and handle

response function.  So to actually use it, you could include this js in

your page.  Then to make one of these backend requests you would tie it

to something.  Like an onclick event or a straight href like this:
 

  <a href="javascript:sndReq('foo')">[foo]</a>
 

That means that when someone clicks on that link what actually happens

is that a backend request to rpc.php?action=foo will be sent.
 

In rpc.php you might have something like this:
 

  switch($_REQUEST['action']) {

    case 'foo':

      / do something /

      echo "foo|foo done";

      break;

    ...

  }
 

Now, look at handleResponse.  It parses the "foo|foo done" string and

splits it on the '|' and uses whatever is before the '|' as the dom

element id in your page and the part after as the new innerHTML of that

element.  That means if you have a div tag like this in your page:
 

  <div id="foo">

  </div>
 

Once you click on that link, that will dynamically be changed to:
 

  <div id="foo">

  foo done

  </div>
 

That's all there is to it.  Everything else is just building on top of

this.  Replacing my simple response "id|text" syntax with a richer XML

format and makine the request much more complicated as well.  Before you

blindly install large "AJAX" libraries, have a go at rolling your own

functionality so you know exactly how it works and you only make it as

complicated as you need.  Often you don't need much more than what I

have shown here.
 

Expanding this approach a bit to send multiple parameters in the

request, for example, would be really simple.  Something like:
 

  function sndReqArg(action,arg) {

    http.open('get', 'rpc.php?action='+action+'&arg='+arg);

    http.onreadystatechange = handleResponse;

    http.send(null);

  }
 

And your handleResponse can easily be expanded to do much more

interesting things than just replacing the contents of a div.
 

-Rasmus

Open in new window

0
 
LVL 2

Accepted Solution

by:
ozanhazer earned 300 total points
Comment Utility
Check http://www.prototypejs.org/api/ajax/updater

Example:
<!-- Simplest form -->

<a href="javascript:;" onclick="new Ajax.Updater('container', 'videos_4_5_and_6.php');">Next Page</a>
 
 

<!-- If you have any javascript in videos_4_5_and_6.php, add the option evalScripts:true -->

<a href="javascript:;" onclick="new Ajax.Updater('container', 'videos_4_5_and_6.php', {evalScripts:true});">Next Page</a>

Open in new window

0
 

Author Comment

by:ryanjameswebb
Comment Utility
ozanhazer: this code works perfectly in FireFox but for some reason doesn't work at all in IE.. Any suggestions?
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
My rather tongue-in-cheek suggestion is, "Don't use IE!"

What version of IE is giving you trouble?  Also, have you tested Safari or Opera?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:ryanjameswebb
Comment Utility
Have tested it on IE 6 through 8.. so 6 7 and 8.  Works fine in safari and opera.

However IE is still a must where it is for a live site, and still 40% of my visitors seem to use IE.
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
Comment Utility
Good - at least it is consistent in that all of IE has trouble.  Notice that the "Rasmus" example treats IE differently from other browsers when it creates the request object.  You might want to look through the JS and be sure that you're hitting the ActiveXObject instead of the XMLHttpRequest when you use IE.
function createRequestObject() {

    var ro;

    var browser = navigator.appName;

    if(browser == "Microsoft Internet Explorer"){

        ro = new ActiveXObject("Microsoft.XMLHTTP");

    }else{

        ro = new XMLHttpRequest();

    }

    return ro;

}

Open in new window

0
 

Author Closing Comment

by:ryanjameswebb
Comment Utility
In order to get everything to work in IE I had to use some of Rasmus's IE code.  Ozanhazer had the right answer hence the 300 points.  But i couldn't have gotten everything working without Rasmus and Ray so they each get 100.  Hope that is fair to all
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
It's quite fair, and thanks for the points.  Glad you're on the right track, ~Ray
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now