I'm doing something like following to transfer value from PHP to javascript:
var str = '<?php echo $v; ?>';
If there is no "'"
in $v
, it'll just be fine.
But in case there is "'"
in $v
, obviously error will be reported.
So far I've only used mysql_real_escape
, which is not applicable now.
您应该使用json_encode()
从PHP转到Javascript:
var str = <?=json_encode($v);?>;
mysql_real_escape_string
is intended to be only used for value to be used in a string in a MySQL query. Use json_encode
to convert your string into a valid JavaScript expression.
<?php
$v = "blah's";
?>
<script language="javascript">
var str = '<?php echo addslashes($v); ?>';
alert(str);
</script>
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.