Can anyone help me why this value from the database wont pass as a parameter
The value from the database has single and double quotes which i escaped using mysql_escape_string() before adding to the database.
<?php
$sample = "This is a test's \"output\".";
?>
<button onclick='myFunction(<?php echo addslashes($sample);?>)'></button>
<script>
function myFunction(val){
alert(val);
}
</script>
Thanks guys!
json_encode($s, JSON_HEX_TAG+JSON_HEX_APOS+JSON_HEX_QUOT)
Use like this way. Hope it will work.
<?php
$sample = "This is a test's \"output\".";
$sample = json_encode($sample);
?>
<button onclick="myFunction(<?php echo htmlentities($sample); ?>)">Button</button>
<script>
function myFunction(val){
alert(val);
}
</script>
Use base64 encode. You encode it in php, it wont have any quotes, and you can decode it in javascript, ie
<?php
$sample = "This is a test's \"output\".";
echo base64_encode($sample);// VGhpcyBpcyBhIHRlc3QncyAib3V0cHV0Ii4=
?>
then
console.log(atob("VGhpcyBpcyBhIHRlc3QncyAib3V0cHV0Ii4=")); // This is a test's "output".
so just:
<button onclick="myFunction(atob(<?php echo base64_encode($sample); ?>))">Button</button>
Further reading:
https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding
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.