admin管理员组文章数量:1290186
I have the following code in php:
<?php
$apples = 123123;
echo "<td>
<input type='button' onclick='confirmation($apples)' value='ac/in'>
</td>";
echo('<script type="text/javascript">
function confirmation(test) {
var answer = confirm("Are you sure?")
if (answer){
alert(test);
$.get("/index.php?f=func_name_to_call",
{name: "John", hobby: "Programming"}, function(data) {});
}
}
</script>');
?>
If $apples is a number then the parameter gets passed correctly; however when it is a string, the function confirmation() is broken. Any ideas? Thanks in advance.
I have the following code in php:
<?php
$apples = 123123;
echo "<td>
<input type='button' onclick='confirmation($apples)' value='ac/in'>
</td>";
echo('<script type="text/javascript">
function confirmation(test) {
var answer = confirm("Are you sure?")
if (answer){
alert(test);
$.get("/index.php?f=func_name_to_call",
{name: "John", hobby: "Programming"}, function(data) {});
}
}
</script>');
?>
If $apples is a number then the parameter gets passed correctly; however when it is a string, the function confirmation() is broken. Any ideas? Thanks in advance.
Share Improve this question edited Jan 23, 2013 at 5:23 Yogesh Pingle 3,6653 gold badges18 silver badges16 bronze badges asked Jan 23, 2013 at 5:18 unwise guyunwise guy 1,1288 gold badges18 silver badges27 bronze badges 1- 1 If you send a string, it must be treated like so. Quote it. – Alfabravo Commented Jan 23, 2013 at 5:21
3 Answers
Reset to default 7If you want it to pass argument as string use the below edited code
echo "<td><input type='button' onclick='confirmation(\"$apples\")' value='ac/in'></td>";
Just use \
for escaping the double quotes,and it will be considered as a string.
Yes you have to use escape character \" \" in calling confirmation function because you pass integer in a integer type parameter which is without double quotes.therefore it converts the string into integer...
<?php
$apples = 123123;
?>
<td>
<input type='button' onclick='confirmation(<?php echo $apples?>)' value='ac/in'>
</td>
<script type="text/javascript">
function confirmation(test) {
var answer = confirm("Are you sure?")
if (answer){
alert(test);
$.get("/index.php?f=func_name_to_call",
{name: "John", hobby: "Programming"}, function(data) {});
}
}
</script>
you'll run into the trouble of converting all those '
into "
since your echoing them by php
, just an advice dont echo
them, use the html
markup and just escape your php
本文标签: phpWhy can39t I pass a string as a parameter in a Javascript functionStack Overflow
版权声明:本文标题:php - Why can't I pass a string as a parameter in a Javascript function? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741460257a2379995.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论