silverglade Posted July 28, 2010 Share Posted July 28, 2010 Hi, I'm trying to do a battle rpg and I need to update the messages when the attack button is pressed, I was wondering, can you output php variables with javascript? My code doesn't work. Any help greatly appreciated. Thanks. Derek <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <?php $battleText="you hit for 100 points!";?> <div id="container"> This is the default text </div> <form><input type="submit" id="link" value="change"/></form> <script type="text/javascript"> var a = document.getElementById('link'); a.onclick = function(){ var d = document.getElementById('container'); var def = d.childNodes[0].nodeValue; t = 5000; setTimeout(function(){ d.childNodes[0].nodeValue = <?php echo $battleText; ?> setTimeout(function(){ d.childNodes[0].nodeValue = "Newer Text"; setTimeout(function(){ d.childNodes[0].nodeValue = def; }, t); }, t); }, t); } </script> </body> </html> Quote Link to comment Share on other sites More sharing options...
Alex Posted July 28, 2010 Share Posted July 28, 2010 You would have to do it like this: d.childNodes[0].nodeValue = "<?php echo $battleText; ?>"; Quote Link to comment Share on other sites More sharing options...
silverglade Posted July 28, 2010 Author Share Posted July 28, 2010 Thank you. I can't get it to work. Is there a way I could add a button to do this code that updates a iframe with variable text? Any more help greatly appreciated. Thank you. Derek <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> <style type="text/css"> body { background-color: black; color: gold; } </style> </head> <body> <html> <body> <script type="text/javascript"> var my_var = "Hello World!" function text2add(){ return '<font color=gold><strong>'+my_var+'</strong></font>'; } </script> <form> <form><input type="submit" value="change" onClick="text2add();"/></form> Main page! Frame below. <br><br> <iframe src="iframe.html" ></iframe> </body> </html> </body> </html> Quote Link to comment Share on other sites More sharing options...
silverglade Posted July 28, 2010 Author Share Posted July 28, 2010 and this are the contents of my iframe page. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <script type="text/javascript"> document.write(parent.text2add()); </script> This is my iframe </body> </html> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.