[英]Passing php variable values to javascript function
I'm passing PHP variable value like this: 我正在传递这样的PHP变量值:
<input type="button" name="delete" value="Remove" onclick="del(<?echo $arr1[0]?>);"/>
del()
is a Javascript function and Eg: $arr1[0]=234-675-90
the value of $arr1[0] is obtained from MYSQL database and its data-type in mysql is varchar. del()
是一个Javascript函数,例如: $arr1[0]=234-675-90
的值是从MYSQL数据库获得的,它在mysql中的数据类型是varchar。
I'm fetching value using mysql_query. 我正在使用mysql_query获取值。
The problem I'm facing is that when value is passed to JavaScript function it takes it wrongly(eg:instead of 234-675-99 as -876).Is there any casting is to be done to pass value from PHP to JavaScript? 我面临的问题是,当将值传递给JavaScript函数时会错误地使用它(例如:代替234-675-99的-876)是否需要进行任何强制转换才能将值从PHP传递给JavaScript?
Any help is greatly appreciated. 任何帮助是极大的赞赏。
您应该将值作为字符串传递:
<input type="button" name="delete" value="Remove" onclick="del('<?echo $arr1[0]?>');"/>
使用json_encode()
将值转换为值JavaScript:
<input type="button" name="delete" value="Remove" onclick="del(<?=htmlspecialchars(json_encode($arr1[0]))?>);"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.