Log in

View Full Version : accessing php generated HTML from javascript



richhelvey
04-04-2006, 10:33 PM
I doubt this is possible based on javascript's security blocks, but is there a way to grab the raw HTML that a php script generates (exactly as if you hit 'view source') and access it through javascript (as text - I don't care about the HTML formatting, actually).

Basically, we've got a web-based order processing system for a graphics department. I'd like to have some local scripts parse the orders for me, but I can't because the HTML is coming from a remote site. Right? (Please tell me I'm wrong.)

Thanks!
-Rich

djr33
04-05-2006, 12:13 AM
Well... I don't know exactly how your stuff is setup, but here's a VERY simple way of doing that:

Note: I don't do JS... command for saving to a variable might be off... I dunno.

<script>
var = "<?php echo "echoed text that's now equal to 'var'"; ?>"
</script>

So.... in short, just make sure the php (or at least the echo statement) is within the place where a variable gets its value... then use that variable to interpret the stuff later.

However, you could just as easily (probably more so) make PHP do the interpretation for you, and you'd be done.

This help?

Twey
04-05-2006, 11:18 AM
By the way you phrased that question, I don't think this is an acceptable solution. Right?
There is one very easy way to get around the Javascript security limitations, which is to serve the required page from the same domain. A simple script:
<?php include("http://www.othersite.com/calculateprices.php"); ?>You can then access the page using an XMLHttpRequest.
function getHttpRequest() {
if(typeof ActiveXObject == "undefined" && typeof XMLHttpRequest == "undefined") return null;
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
catch (e) {
try xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
catch (E) xmlhttp = false;
}
@else xmlhttp = null;
@end @*/

if (xmlhttp == null && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = null;
}
}
return xmlhttp;
}

// ...
function getPHPOutput() {
(var xh = getHttpRequest()) == null ? return false : 0;
xh.open("GET", "getpage.php", false);
xh.send(null);
if(xh.readyState != 4) return false;
else return xh.responseText;
}