简体   繁体   中英

PHP Echoing JSON string into HTML Input value - Need character escape

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

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

<input ... value="<?php echo htmlspecialchars(json_encode($whatever)); ?>" />
                             ^^^^^^^^^^^^^^^^----
$val= json_encode($val);
<input type="hidden" value="<?php echo htmlspecialchars($val); ?>" name="bye">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM