3

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.

1
  • use htmlspecialchars($string) Commented Feb 13, 2013 at 18:00

1 Answer 1

6

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

$text = htmlspecialchars($_POST['text']);
echo $text;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.