GlobalJim
GlobalJim

Reputation: 1189

Why aren't <script> tags not working on this PHP page?

When I try to enter

<script type="text/javascript" >
alert("hello");
</script>

in the comment box on my PHP page I do not get an alert box. I see the script in my text file, not on the webpage. For some reason the <script> isn't executing. I have active scripting and javascript enabled on all my browsers.

My PHP code:

<?php //CFPcomments.php

include_once 'CFPheader.php';




if (isset($_POST['content']))
{
    $fp = fopen('comments.txt', 'a');
    fwrite($fp, $_POST['content'] . "<br />");
    fclose($fp);
}

echo nl2br(file_get_contents('comments.txt'));





echo <<<_END
<h3>Post comment</h3>
<form action='CFPcomments.php' method='post'>
<textarea name='content' rows ='3' cols='100'></textarea>
<br/>
<input type='submit' value='Post' />
</form>
_END;
?>

Strange. I got it to work, not sure why.

    <!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript">
        alert("hello");
    </script>
</head>
<body>

</body>
</html>

When I type this in it seems to work

Anyone have any idea why???? Very confused.

Upvotes: 0

Views: 2481

Answers (2)

GlobalJim
GlobalJim

Reputation: 1189

I assigned a variable to the content, then displayed the variable in the PHP code. Now it works.

<?php //CFPcomments.php

include_once 'CFPheader.php';


setcookie("username", $GLOBALS['user'], time()+3600);
setcookie("password", $GLOBALS['pass'], time()+3600);
if (isset($_POST['content']))
{
    $fp = fopen('comments.txt', 'a');
    fwrite($fp, $_POST['content'] . "<br />");
    fclose($fp);
}

$comment =  file_get_contents('comments.txt');





echo <<<_END
<h3>Post comment</h3>
'$comment'
<form action='CFPcomments.php' method='post'>
<textarea name='content' rows ='3' cols='100'></textarea>
<br/>
<input type='submit' value='Post' />
</form>
_END;
?>

Upvotes: 0

Scuzzy
Scuzzy

Reputation: 12332

your nl2br() is most likely translating

<script type="text/javascript" >
alert("hello");
</script>

to

<script type="text/javascript" ><br/>
alert("hello");<br/>
</script><br/>

and breaking the JavaScript code.

Upvotes: 4

Related Questions