想在 javascript 函数中传递 php 变量作为参数吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4050218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Want to pass php variable as argument in javascript function?
提问by Nitz
i want to pass my php variable in one javascript function.
i know it seems simple but i don't know where am i missing something?
我想在一个 javascript 函数中传递我的 php 变量。
我知道这看起来很简单,但我不知道我在哪里遗漏了什么?
<?php
while($gg=mysql_fetch_array($lg))
{
?>
<td id="add_td">
<?php
$id = $gg['p_id'];
echo "<a onclick=cnf('Are you sure you want to delete that?',$id)>"; ?>Delete</a>
</td>
<?php
}
?>
and in my javascript function
在我的 javascript 函数中
function cnf(msg,id)
{
cnf = confirm(msg);
if(cnf) {
parent.location.href = 'p_update.php?d=' + id;
}
}
so i need to know on which id that user had clicked so that i will only delete that id from database.
if i try this thing then it showing error on "cnf" function and its saying like "unterminated string literal"?
所以我需要知道用户点击了哪个 ID,以便我只会从数据库中删除该 ID。
如果我尝试这个东西,那么它会在“cnf”函数上显示错误,并且它的说法类似于“未终止的字符串文字”?
回答by Eldar
if $id is not numeric you should write
<?php
while($gg=mysql_fetch_array($lg))
{
?>
<td id="add_td">
<?php
$id = $gg['p_id'];
echo "<a onclick=cnf('Are you sure you want to delete that?','".$id."')>"; ?>Delete</a>
</td>
<?php
}
?>
回答by mplungjan
I would do something like this instead. HREF is mandatory if you want the "hand" pointer A unique ID is also mandatory on tags and you need to quote the ID if you pass it in the function instead of what I suggest and give the link the id
我会做这样的事情。如果您想要“手”指针,则 HREF 是强制性的 标签上也必须有唯一的 ID,如果您在函数中传递它而不是我建议的 ID 并为链接提供 ID,则您需要引用 ID
function cnf(link,id) {
if (confirm("Are you sure you want to delete "+id) {
link.href = "p_update.php?d=" + id;
return true;
}
return false;
}
<?php
while($gg=mysql_fetch_array($lg)) {
$id = $gg['p_id'];
?>
<td id="add_td<?php echo $id; ?>"><a target="_parent" href="#" id="<?php echo $id; ?>" onclick="return cnf(this)">Delete</a></td>
<?php } ?>
回答by Alex
foreach($mas as $k=>$v) {
//echo $k.' = '.$v.'<br>';
echo("
<input type=\"button\" id=\"delete_id".$k."\" value=\"Blablabla\" onclick=\"alert('".$v."');\"/>
");
}
If I put there $k (index) it works but if string (value is string), I get an error
如果我把 $k (index) 放在那里,它可以工作,但是如果 string (value is string),我会收到一个错误
unterminated string literal
未终止的字符串文字
In HTML tags this works but not as the function argument!
在 HTML 标签中,这有效,但不能作为函数参数!
回答by Viren
Use quotes around php variable '$id'.
在 php 变量 '$id' 周围使用引号。
echo "<a onclick=cnf('Are you sure you want to delete that?','$id')>";
Another example,
$pis some php variable,
另一个例子,
$p是一些php变量,
echo "<input type=button value='update' onclick=myfunc('$p')>"
回答by Chinmayee G
Check your syntax
检查你的语法
<?php
while($gg=mysql_fetch_array($lg))
{
?>
<td id="add_td">
<?php
$id = $gg['p_id'];
?>
<a onclick="cnf('Are you sure you want to delete that?',<?=$id?>);">Delete</a>
</td>
<?php
}
?>
回答by Kel
If $id is a string literal, you should put it into quotes when you are passing it as a parameter to cnf() function in <a ... > tag:
如果 $id 是字符串文字,则在将其作为参数传递给 <a ... > 标签中的 cnf() 函数时,应将其放入引号中:
echo "<a onclick=cnf('Are you sure you want to delete that?', '$id')>";

