Jump to content

can I output php variables with javascript?


silverglade

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.