?
Solved

Dynamically Loading External PHP into a DIV using Prototype or scriptaculous

Posted on 2009-04-10
8
Medium Priority
?
2,414 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 800 total points
ID: 24122928
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 1200 total points
ID: 24125642
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
ID: 24131261
ozanhazer: this code works perfectly in FireFox but for some reason doesn't work at all in IE.. Any suggestions?
0
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24132638
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
 

Author Comment

by:ryanjameswebb
ID: 24133043
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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 800 total points
ID: 24133633
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
ID: 31623687
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 111

Expert Comment

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

765 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