简体   繁体   English

在php echo语句中编写变量时如何通过javascript函数传递变量

[英]how to pass the variables through a javascript function when it is written inside the php echo statement

how to pass the variables through a javascript function when it is written inside the php echo statement . 在php echo语句中编写变量时如何通过javascript函数传递变量。

here my code 这是我的代码

problem with quotes 报价问题

 echo '<a style="color:green" href="javascript:void(0)" onclick="javascript:chatWith('$u_code','$u_name')" >'.$u_name.'</a>';

The cleanest option by using \\DOMDocument : 使用\\DOMDocument最干净的选项:

<?php
$dom = new DOMDocument;
$e = $dom->createElement('a', $u_name);
$a = $dom->appendChild($e);
$a->setAttribute('style',   'color: green;');
$a->setAttribute('href',    'javascript:void(0);');
$a->setAttribute('onclick', 'chatWith("' . $u_code . '","' . $u_name . '");');

echo $dom->saveHTML();

您需要转义那些引号:

echo '<a style="color:green" href="javascript:void(0)" onclick="javascript:chatWith(\''.$u_code.'\',\''.$u_name.'\')" >'.$u_name.'</a>';

You need to append your php variables with "." 您需要在php变量后附加“。” and escape quotes 和转义引号

 echo '<a style="color:green" href="javascript:void(0)" onclick="javascript:chatWith(\''.$u_code.'\',\''.$u_name.'\')" >'.$u_name.'</a>';
echo '<a style="color:green" href="javascript:void(0)" onclick="javascript:chatWith('.$u_code.','.$u_name.')" >'.$u_name.'</a>';

修正报价

echo '<a style="color:green" href="javascript:void(0)" onclick="javascript:chatWith(\''.$u_code.'\',\''.$u_name.'\')" >'.$u_name.'</a>';
echo "<a style=\"color:green\" 
         href=\"javascript:void(0)\" 
         onclick=\"javascript:chatWith('".$u_code."','".$u_name."')\" 
         >'.$u_name.'</a>";

转义语句中的引号。

echo '<a style="color:green" href="javascript:void(0)" onclick="javascript:chatWith(\''.$u_code.'\',\''.$u_name.'\')" >'.$u_name.'</a>';

Sometimes, we should just close PHP, improves readability AND you don't get stuck in nested quotes. 有时,我们应该关闭PHP,提高可读性,并且不会陷入嵌套的引号中。

?>

<a style="color: green"
   href="javascript: void(0);"
   onclick="javascript: chatWith('<?php echo $u_code; ?>','<?php echo $u_name; ?>');">
    <?php echo $u_name; ?>
</a>

<?php

You can do this, which is cleaner 您可以这样做,这更干净

$link = '<a onclick="javascript:chatWith({ucode},{uname})" style="color:green" href="javascript:void(0)">{anchor}</a>';

 $link = str_replace(
 $q = "'";
array('{ucode}', '{uname}', '{anchor}'), 
      array($q.$u_code.$q, $q.$u_name.$q, $uname), 
      $link
    );

echo $link;

用这个

echo '<a style="color:green" href="javascript:void(0)" onclick="javascript:chatWith('.$u_code.','.$u_name.')" >'.$u_name.'</a>';

Your formatting seems to be off. 您的格式似乎已关闭。

echo '<a style="color:green" href="javascript:void(0)" onclick="javascript:chatWith(\''.$u_code.'\',\''.$u_name.'\')" >'.$u_name.'</a>';

the . . is used to append variables to a string in PHP, since you are using single quotes you'll have to escape the string every time you insert a variable. 用于在PHP中将变量附加到字符串中,因为您使用的是单引号,所以每次插入变量时都必须对字符串进行转义。

Hope this helped. 希望这会有所帮助。

‐ Sid ‐ Sid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM