Execute JavaScript/DOM before PHP

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Bughy
Forum Newbie
Posts: 3
Joined: Mon Aug 25, 2008 5:04 am

Execute JavaScript/DOM before PHP

Post by Bughy »

I have this function:

Code: Select all

 
<head>
<script type="text/javascript">
function change(){
    var x = document.getElementById("myCode");
    var y = x.innerHTML;
    y= y.replace(/</g, "<")
    y= y.replace(/>/g,">")
    document.getElementById('myCode').innerHTML = y;
}
</script>
</head>
<body onload="change();">
 
<div id="myCode">
<?php echo 'Batman'; ?>
</div>
 
</body>
 
If I write simple HTML code in myCode div, works perfect, but when I write PHP code, it gets executed, and there's nothing to replace anymore.

If you haven't already figured out, I want to replace the "<" and the ">", so the code is being displayed, not executed.
Any suggestions?
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Re: Execute JavaScript/DOM before PHP

Post by matthijs »

PHP is always executed first. So you'll have to do the conversion server side. Maybe with some preg_replace or htmlentities

Code: Select all

 
echo htmlentities('<?php echo \'Batman\'; ?>'); // produces <?php echo 'Batman'; ?>
 
edit: the comment view here removed the slashes \ I put in front of the ' surrounding Batman.
Bughy
Forum Newbie
Posts: 3
Joined: Mon Aug 25, 2008 5:04 am

Re: Execute JavaScript/DOM before PHP

Post by Bughy »

uhm, not good, it shouldn't request any other writing besides the actual code.
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Re: Execute JavaScript/DOM before PHP

Post by matthijs »

I don't understand what you want to do then. Maybe you could explain that.
Bughy
Forum Newbie
Posts: 3
Joined: Mon Aug 25, 2008 5:04 am

Re: Execute JavaScript/DOM before PHP

Post by Bughy »

i want to rewrite myCode div, all that's written in it
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Re: Execute JavaScript/DOM before PHP

Post by matthijs »

Post Reply