[英]Call javascript function using php variable
I am using the below code to call the javascript function from php script. 我正在使用以下代码从php脚本调用javascript函数。 Its not working while am adding the php variable in javascript($msg).
我在javascript($ msg)中添加php变量时无法正常工作。 Please help me to do this.
请帮我做到这一点。
if ( isset($_GET['Error'])) {
$msg =$_GET["Error"];
echo '<script type="script.php">validate("Error",$msg);</script>';
}
You have to quote the $msg
or it will be syntax error in javascript. 您必须引用
$msg
,否则它将是javascript中的语法错误。 And the type
is non sense. 而且
type
是没有意义的。
Since the msg is from the $_GET
, don't forget the escape it. 由于味精来自
$_GET
,请不要忘记对其进行转义。
if ( isset($_GET['Error'])) {
$msg =$_GET["Error"];
echo '<script>validate("Error", "'.htmlspecialchars($msg).'");</script>';
}
Instead of an inline script, this should work: 代替内联脚本,这应该可以工作:
<html>
<head>
<script type="text/javascript">
function testMe(x){
alert(x);
}
</script>
</head>
<body>
<?php
$phpVar = 'I am a PHP variable';
echo "<a href=\"#\" onclick=\"javascript:testMe('" . $phpVar . "');\">Click Me</a>";
?>
</body>
<html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.