5

Here is a simplified version of my code that I am having a problem with.

$variable = "{\\\"JSON" //long JSON string created in Javascript with JSON.stringify
?> <input type="text" name="somename" value="<?php echo $variable; ?>"/> <?php

The input box only contains {\ I need a way to escape the entire JSON string

Thanks Alex

2 Answers 2

23

You're outputting into an HTML context, so you need html-specific escaping:

<input ... value="<?php echo htmlspecialchars(json_encode($whatever)); ?>" />
                             ^^^^^^^^^^^^^^^^----
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks body I simply use this function htmlspecialchars($json_value) inside input value, save my time.
-1
$val= json_encode($val);
<input type="hidden" value="<?php echo htmlspecialchars($val); ?>" name="bye">

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.