[英]How to pass php variable to JavaScript function as parameter
I want to pass php image url as javascript function parameter. 我想将php图片网址作为javascript函数参数传递。
Please let me know how can I do this. 请让我知道我该怎么做。
<html> <?php $image=$row['image']; $img=$loc.'user/'.$image; ?> <script type=text/javascript> function demo() { some code here; } </script> <body> <button type="submit" onclick=demo(<?php echo $img?>) > </body> </html>
In javascript I am doing some editing work on image . 在javascript中,我正在对图片进行一些编辑工作。
How can I pass this php variable (ie image url as javascript function parameter) 如何传递此php变量(即,图像url作为javascript函数参数)
You need to enclose the PHP code within quotes
like as 您需要将PHP代码用
quotes
例如
<button type="submit" onclick="demo('<?php echo $imageurl;?>');" >
//^^ ^^
Within your Javascript as you were passing value within function but not capturing that value within your function demo()
当您在函数中传递值但未在
function demo()
捕获该值时,在Javascript中
function demo(obj)
{ //^^^ Capturing value within function using variable name obj
alert(obj);
}
您必须用引号将其包装,并且必须关闭button
<button type="submit" onclick='demo("<?php echo $imageurl?>")' ></button>
<button type="submit" onclick="demo('<?php echo $img;?>');" >
I found $img
is your variable and in code you are trying to send $imageurl
我发现
$img
是您的变量,并且在代码中您尝试发送$imageurl
<html>
<?php
$image=$row['image'];
$img=$loc.'user/'.$image;
?>
<script>
function demo(val1)
{
alert(val1);
}
</script>
<body>
<button type="submit" onclick=demo('<?php echo $img?>') >Click Me</button>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.