PDA

View Full Version : Not so messy javascript



hosdank
08-21-2008, 09:39 PM
I have a table which contains quite a bit of PHP code(Don't worry, this question IS related to JavaScript). When the user clicks a button I need the PHP code to change. I know I can use getElementById('shade').innerHTML='(PHP CODE INSERTED HERE)' to change it, but the thing is it would be a pain to keep all my PHP code on one line due to it being all wrapped inside a JavaScript command. I'd like to be able to format it with new lines and every thing. Any ideas? Sorry if that wasn't explained well. Thanks :)

Nile
08-21-2008, 09:41 PM
You can actually use file_get_contents in js with a little bit of PHP, search it:
http://www.google.com/search?q=file_get_contents+in+js&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a
With that you can use: getElementById(...).innderHTML=file....

hosdank
08-21-2008, 09:43 PM
Thanks so much! :)

hosdank
08-21-2008, 10:12 PM
Any idea why this won't work? It just dosn't do anything

<script language='javascript'>
function file_get_contents( url )
{
// http://kevin.vanzonneveld.net
// + original by: Legaev Andrey
// % note 1: This function uses XmlHttpRequest and cannot retrieve resource from different domain.
// * example 1: file_get_contents('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm');
// * returns 1: '123'

var req = null;
try { req = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {
try { req = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {
try { req = new XMLHttpRequest(); } catch(e) {}
}
}
if (req == null) throw new Error('XMLHttpRequest not supported');

req.open("GET", url, false);
req.send(null);

return req.responseText;
}

var filecontents;
filecontents=file_get_contents('http://localhost/spacetrade/HTML & Code/blah.php');
document.write(filecontents);
</script>

Nile
08-21-2008, 11:05 PM
I don't know, but the error says its a security issue, please try one of the other codes on Google I linked you to.
I've also found this one that looks quite promising:
http://forums.tizag.com/showthread.php?t=3496

hosdank
08-22-2008, 06:20 PM
Thanks :)