views:

44

answers:

3

I have a div on the page where the contents change depending on the value selected in a drop down. Because there is a large amount of formatted content in this div I want to have a php script supply html which is stored in a file on the system.

I can't seam to figure out how to have php return the contents of that file, which essentially contains many div tags and content, as text to the ajax script. Which would then change the content in the div on the page.

The back end php is using CakePHP and using YUI3 as the front end framework.

A solution doesn't need to include any Cake if need be.

Thanks!

+1  A: 

readfile()

Ignacio Vazquez-Abrams
Ignacio - you sure get to the point fast!
meouw
+1  A: 

"I can't seam to figure out how to have php return the contents of that file"

echo file_get_contents( $filepath );

For the ajax part in the website (although you don't seem to ask for that):

var xmlhttp;
if( window.XMLHttpRequest )
    xmlhttp = new XMLHttpRequest();
else if( window.ActiveXObject )
    xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" );     // Use ActiveX in IE
else {
    alert( 'Your browser doesn\'t support XMLHttpRequest.' );
    return;
}
xmlhttp.eid = eid; // <-- id of the div to be filled with the content from the file
xmlhttp.onreadystatechange = function() {
    if( xmlhttp.readyState == 4 ) {
        document.getElementById( xmlhttp.eid ).innerHTML = xmlhttp.responseText;
    }
}
xmlhttp.open( "GET", "phpfile.php", true );
xmlhttp.send( null );

Edit: Please consider that this solution may not be the way it's done with your framework.

svens
Thanks - your method works as well! I am using YUI3 so the AJAX stuff is already handled. +1
A: 

You can also create view for each type of html form and return it through ajax helper. It will give you more flexibility; you can even add some php logic to this html form and so on...

Storing formatted html form in file is too primitive because it is really easy to imagine the situation when you'll need to fill some tags with content from your DB. Doing it with "view" will give you this opportunity.

Best regards.

Kirzilla
Yeah we have thought about that option but in this situation the best case is to store a static html file. Thanks though.