I need to print a string JSON
from PHP
inside value
a input
, so I can retrieve it later with JavaScript
.
<?php
$array = array(
"foo" => "bar",
);
$json = json_encode($array);
?>
<input id="input" value="<?php echo $json; ?>" />
<script>
let json = JSON.parse(document.getElementById('input').value);
console.log(json);
</script>
What would I have to do to be able to print the JSON
and later be able to retrieve it from JavaScript
?
What is needed is to escape the characters
HTML
to prevent this from breaking when printing theJSON
.PHP
It has the functionhtmlspecialchars
which:Solution: