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.
asked Feb 13, 2013 at 17:58
tejashsoni111
1,4051 gold badge18 silver badges34 bronze badges
-
use htmlspecialchars($string)user1646111– user16461112013年02月13日 18:00:01 +00:00Commented Feb 13, 2013 at 18:00
1 Answer 1
This can be achieved by using htmlspecialchars (reference).
Off the top of my head:
$text = htmlspecialchars($_POST['text']);
echo $text;
answered Feb 13, 2013 at 18:00
James Donnelly
129k35 gold badges215 silver badges224 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default