admin管理员组文章数量:1289620
I cant use PHP variables to use as parameters for my function.
The function is:
<script type="text/javascript">
function myFunction(id,name) {
alert(id);
alert(name);
}
</script>
And this is PHP part:
echo "<button onclick='myFunction($intvar,$strvar)' type='button'>ClickMe</button>";
the call works when I use numbers like this: myFunction(0,1) but I need to use variables to call myFunction.
Please Help, thanks.
I cant use PHP variables to use as parameters for my function.
The function is:
<script type="text/javascript">
function myFunction(id,name) {
alert(id);
alert(name);
}
</script>
And this is PHP part:
echo "<button onclick='myFunction($intvar,$strvar)' type='button'>ClickMe</button>";
the call works when I use numbers like this: myFunction(0,1) but I need to use variables to call myFunction.
Please Help, thanks.
Share Improve this question edited Jun 25, 2015 at 6:23 Roberto Sepúlveda Bravo asked Jun 25, 2015 at 6:18 Roberto Sepúlveda BravoRoberto Sepúlveda Bravo 8562 gold badges17 silver badges29 bronze badges 1- 2 change alert(ide); to alert(id); – Vivek Singh Commented Jun 25, 2015 at 6:19
2 Answers
Reset to default 6You try this
function myFunction(ide,name) {
change insted of id => ide
echo "<button onclick='myFunction(".'"'.$intvar.'","'.$strvar.'"'.")' type='button'>ClickMe</button>";
Good practice is to echo json encoded version of variables, this way you ensure that strings are properly quoted:
echo "<button onclick='myFunction(" . json_encode($intvar) . "," . json_encode($strvar, JSON_HEX_APOS) . ")' type='button'>ClickMe</button>";
本文标签: phpCall JavaScript function(with parameters) by onclick eventStack Overflow
版权声明:本文标题:php - Call JavaScript function(with parameters) by onclick event - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741466253a2380330.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论