在 onClick 函数中传递 php 变量
我想在 onClick 函数中传递 php 变量值。 当我传递 php 变量时,在 UI 中我得到的是变量本身,而不是我需要变量中的值。
下面是代码片段,请帮助我。
<?php
print '<td>';
$node = $name->item(0)->nodeValue;
$insert= "cubicle"."$node<br>";
Echo '<a href= "#" onClick= showDetails("$node");>'. $insert .'</a> ';
print '</td>';
?>
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.
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>';
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
变量解析仅在双引号字符串中完成。您可以使用字符串连接,或者我认为更具可读性的
printf
[docs]:最好的方法是根本不
echo
HTML,而是将 PHP 嵌入 HTML:您必须少考虑引号并且调试 HTML 也更容易。
注意:如果
$node
表示数字,则不需要在参数两边加上引号。Variable parsing is only done in double quoted strings. You can use string concatenation or, what I find more readable,
printf
[docs]:The best way would be to not
echo
HTML at all, but to embed PHP in HTML:You have to think less about quotes and debugging your HTML is easier too.
Note: If
$node
is representing a number, you don't need quotations marks around the argument.你不应该将 $node 包装在 '"' 中:
如果你希望 $node 的值位于字符串中,我会这样做:
you shouldn't be wrapping $node in '"':
If you want the value of $node to be in a string, thn i would do:
不要混淆
"
和'
,它们都很重要。如果您在字符串中使用了一些"
并且不想使用与分隔符相同的字符,使用这个技巧: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:我认为您正在寻找 PHP 函数
json_encode
,它将 PHP 变量转换为 JavaScript 对象。它比在输出中直接传递值更安全。
I think you are searching for PHP function
json_encode
which converts PHP variable into JavaScript object.It's more secure than passing the value right in the output.
我最近一直在使用花括号而不是串联。我认为它看起来更好/更具可读性,而且大多数情况下我发现它更容易并且不太容易出现人为错误 - 保持所有这些引用都是直的!您还需要在 onClick 内部的内容周围加上引号。
而不是这个:
尝试这个:
作为旁注,我通常使用双引号来包装我的 echo 语句,并在其中严格使用单引号。但这只是我的风格。不管你怎么做,一定要保持正直。所以我的版本看起来像这样:
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.
Instead of this:
Try this:
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: