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> Link to comment https://forums.phpfreaks.com/topic/209139-can-i-output-php-variables-with-javascript/ 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; ?>"; Link to comment https://forums.phpfreaks.com/topic/209139-can-i-output-php-variables-with-javascript/#findComment-1092263 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> Link to comment https://forums.phpfreaks.com/topic/209139-can-i-output-php-variables-with-javascript/#findComment-1092286 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> Link to comment https://forums.phpfreaks.com/topic/209139-can-i-output-php-variables-with-javascript/#findComment-1092290 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.