Javascript 在onClick函数中传递php变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6621231/
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
passing php variable in onClick function
提问by Vinod HC
I want to pass the php variable value in onClick function. When i pass the php variable, in the UI i am getting the variable itself instead I need the value in the variable.
我想在 onClick 函数中传递 php 变量值。当我传递 php 变量时,在 UI 中我得到的是变量本身,而不是我需要变量中的值。
Below is code snippet, please help me.
下面是代码片段,请帮助我。
<?php
print '<td>';
$node = $name->item(0)->nodeValue;
$insert= "cubicle"."$node<br>";
Echo '<a href= "#" onClick= showDetails("$node");>'. $insert .'</a> ';
print '</td>';
?>
回答by Felix Kling
Variable parsing is only done in double quoted strings. You can use string concatenation or, what I find more readable, printf
[docs]:
变量解析只在双引号字符串中完成。您可以使用字符串连接,或者我觉得更易读的printf
[docs]:
printf('<a href= "#" onClick="showDetails(\'%s\');">%s</a> ', $node, $insert);
The best way would be to not echo
HTML at all, but to embed PHP in HTML:
最好的方法是根本不使用echo
HTML,而是将 PHP 嵌入到 HTML 中:
<?php
$node = $name->item(0)->nodeValue;
$insert = "cubicle" . $node;
?>
<td>
<a href= "#" onClick="showDetails('<?php echo $node;?>');">
<?php echo $insert; ?> <br />
</a>
</td>
You have to think less about quotes and debugging your HTML is easier too.
您不必考虑引号,调试 HTML 也更容易。
Note:If $node
is representing a number, you don't need quotations marks around the argument.
注意:如果$node
代表一个数字,则参数周围不需要引号。
回答by Jim Deville
you shouldn't be wrapping $node in '"':
您不应该将 $node 包装在 '"' 中:
Echo '<a href= "#" onClick= showDetails($node);>'. $insert .'</a> ';
If you want the value of $node to be in a string, thn i would do:
如果您希望 $node 的值在一个字符串中,那么我会这样做:
Echo '<a href= "#" onClick= showDetails("' . $node. '");>'. $insert .'</a> ';
回答by SteeveDroz
$var = "Hello World!";
echo "$var"; // echoes Hello World!
echo '$var'; // echoes $var
Don't mix up "
and '
, they both have importance. If you use some "
in your string and don't want to use the same character as delimiter, use this trick:
不要混淆"
和'
,它们都很重要。如果您"
在字符串中使用 some并且不想使用与分隔符相同的字符,请使用以下技巧:
echo 'I say "Hello" to ' . $name . '!';
回答by Praveen Prasad
Echo '<a href= "#" onClick= showDetails("'.$node.'");>'. $insert .'</a> ';
回答by c0gent
I have been using curly braces lately instead of concatenation. I think it looks better/is more readable, and mostly I find it is easier and less prone to human error - keeping all those quotes straight! You will also need quotes around the contents inside of onClick.
我最近一直在使用花括号而不是串联。我认为它看起来更好/更具可读性,而且大多数情况下我发现它更容易并且更不容易出现人为错误 - 保持所有这些引用直!您还需要在 onClick 内的内容周围加上引号。
Instead of this:
取而代之的是:
Echo '<a href= "#" onClick= showDetails($node);>'. $insert .'</a> ';
Try this:
尝试这个:
Echo '<a href= "#" onClick= "showDetails({$node});">{$insert}</a> ';
As a side note, I usually use double quotes to wrap my echo statement and strictly use single quotes within it. That is just my style though. However you do it, be sure to keep it straight. So my version would look like this:
作为旁注,我通常使用双引号来包裹我的 echo 语句并在其中严格使用单引号。不过这只是我的风格。不管你怎么做,一定要保持笔直。所以我的版本看起来像这样:
echo "<a href='#' onClick='showDetails({$node});'>{$insert}</a>";
回答by Gedrox
I think you are searching for PHP function json_encode
which converts PHP variable into JavaScript object.
我认为您正在搜索json_encode
将 PHP 变量转换为 JavaScript 对象的PHP 函数。
It's more secure than passing the value right in the output.
它比在输出中正确传递值更安全。