开发者

PHP sending HTML to JS

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 solu开发者_如何学运维tion doesn't need to include any Cake if need be.

Thanks!


readfile()


"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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜