tejashsoni111
tejashsoni111

Reputation: 1405

Display javascript code as string in php file

I am trying to echo javascript code in php. I want to show the java script code as string, not to be executed. I have following code in temp.php file:

<?php
if (count($_POST) > 0)
{
    $text = $_POST['text'];
    echo $text;
}?>

<html>
<head>
</head>
<body>
    <form action="temp.php" method="post">
        Text : <br /><textarea name="text" rows="3" cols="50"></textarea>
        <br />
        <input type="submit" value="Submit" />
    </form>
</body>
</html>

I am taking following input:

<script>alert("hello");</script>

when I am submitting the form, browser is executing the javascript and giving alert message, but I want to print code as string. I have tested this code in "firefox". Please give me some reference. Thank you.

Upvotes: 3

Views: 1221

Answers (1)

James Donnelly
James Donnelly

Reputation: 128791

This can be achieved by using htmlspecialchars (reference). Off the top of my head:

$text = htmlspecialchars($_POST['text']);
echo $text;

Upvotes: 6

Related Questions