[英]Pass ID on onClick Function in Javascript
I am trying to pass the email using the onClick function and I want to show that email in alert: 我正在尝试使用onClick函数传递电子邮件,并且希望在警报中显示该电子邮件:
<td>
<button class="btn btn-primary" name="buton" onclick="myfunction(<?php $email; ?>)" >Edit</button>
</td>
Here is my function: 这是我的功能:
<script type="text/javascript">
function myfunction(id)
{
alert(id);
}
</script>
function myfunction(id) { alert(id); }
<td> <button class="btn btn-primary" name="buton" onClick="myfunction('<?php echo $email; ?>')" >Edit</button></td>
You need to put the email in 'single quotes' 您需要将电子邮件放在“单引号”中
当您传递电子邮件(字符串)时,请尝试此操作,它应该在quote
,而在JavaScript中使用php时,在这种情况下,您必须使用echo
<td><button class="btn btn-primary" name="buton" onclick="myfunction('<?php echo $email; ?>')" >Edit</button></td>
What you want is probably the PHP tag <?=
that echoes the $email variable as an argument to the JavaScript function. 您想要的可能是PHP标记
<?=
,该标记回显$ email变量作为JavaScript函数的参数。 Note that you will need quotation marks for the email: 请注意,您将需要在电子邮件中使用引号:
onclick='myfunction("<?= $email ?>")'
If shorttags are not enabled the long version must be used: 如果未启用短标签,则必须使用长版本:
onclick='myfunction("<?php echo $email; ?>")'
You've to make two changes to validate your code : 您必须进行两项更改以验证您的代码:
You should add echo
to pass the php variable to JS. 您应该添加
echo
以将php变量传递给JS。
You need to add single quotes since the $email
is a string and you've already using double quotes in onclick
attribute. 您需要添加单引号,因为
$email
是一个字符串,并且您已经在onclick
属性中使用了双引号。
<td> <button class="btn btn-primary" name="buton" onclick="myfunction('<?php echo $email; ?>')" >Edit</button> </td>
Your code is working, you are just not echoing the value of $email
in a good way: 您的代码正在运行,只是没有以一种很好的方式回显
$email
的值:
- Lack of the single quotes in myfunction(' … ')
myfunction(' … ')
缺少单引号
- Lack of echo
inside your php tags myfunction('<?php echo $email ?>')
-在您的php标签中缺少
echo
myfunction('<?php echo $email ?>')
To avoid that kind of problems… 为了避免这种问题……
In your php, I suggest you to use heredoc syntax: 在您的php中,建议您使用heredoc语法:
echo <<< EOD
<td><button class="btn btn-primary" name="buton" onclick="myfunction('{$email}')">Edit</button></td>
EOD;
Note the { }
around the variable. 注意变量周围的
{ }
。 They are not necessary, but I suggest you to use them, as they make the variables more noticable. 它们不是必需的,但我建议您使用它们,因为它们会使变量更明显。
⋅ ⋅ ⋅ ⋅⋅⋅
The result will be: 结果将是:
function myfunction(id) { alert(id); }
<td><button class="btn btn-primary" name="buton" onclick="myfunction('myemail@stackoverflow.com')">Edit</button></td>
Documentation about Heredoc syntax: http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc 有关Heredoc语法的文档: http : //php.net/manual/zh/language.types.string.php#language.types.string.syntax.heredoc
I hope it helps. 希望对您有所帮助。
回显php标签中的变量
<td><button class="btn btn-primary" name="buton" onclick="myfunction('<?php echo $email; ?>')" >Edit</button></td>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.