I am using the below code to call the javascript function from php script. Its not working while am adding the php variable in javascript($msg). Please help me to do this.
if ( isset($_GET['Error'])) {
$msg =$_GET["Error"];
echo '<script type="script.php">validate("Error",$msg);</script>';
}
asked Jan 17, 2014 at 4:01
user3085540
2953 gold badges12 silver badges27 bronze badges
2 Answers 2
You have to quote the $msg or it will be syntax error in javascript.
And the type is non sense.
Since the msg is from the $_GET, don't forget the escape it.
if ( isset($_GET['Error'])) {
$msg =$_GET["Error"];
echo '<script>validate("Error", "'.htmlspecialchars($msg).'");</script>';
}
answered Jan 17, 2014 at 4:12
xdazz
161k38 gold badges255 silver badges278 bronze badges
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>
answered Jan 17, 2014 at 4:17
Eric
1,0201 gold badge12 silver badges18 bronze badges
Comments
default
<script type="script.php">isn't a valid value for thetypeattribute. Changescript.php