[英]Get my database value as a string to pass it to my javascript function PHP
I have an string value from my database which is the id and I have a button with a javascript function onclick="values('argument') ". 我有一个来自数据库的字符串值,它是id ,我有一个带有javascript函数onclick =“ values('argument') ”的按钮。
My problem is that when i tried to click on that button i have this error: 我的问题是,当我尝试单击该按钮时,出现此错误:
(index):1 Uncaught ReferenceError: argument is not defined
I know this happens because the argument is not treat as a string. 我知道发生这种情况是因为参数没有被视为字符串。
How can I pass the argument as a string? 如何将参数作为字符串传递?
All this is a table which is contained in a php function that shows info from my database 所有这些都是一个包含在php函数中的表,该表显示了我数据库中的信息
This is my complete code. 这是我完整的代码。
<?php
function oportunities($resultQuery)
{
echo '<div class="table-responsive">
<table class="responstable">
<thead class="thead">
<tr>
<th>No.</th>
<th>value1</th>
<th>value2</th>
<th>value3</th>
<th>value4</th>
<th>value5</th>
<th>value6</th>
<th>value7</th>
<th>value8</th>
<th>value9</th>
<th>value10</th>
<th>value11</th>
<th>value12</th>
<th>value13</th>
<th>value14</th>
<th>value15</th>
</tr>
</thead>
<tbody>';
foreach($resultQuery as $row){
echo '<tr>
<td> '. $row['id'] .'</td>
<td>'. $row['value1'] .'</td>
<td>'. $row['value2'] .'</td>
<td class="descripcion">
<p class="text-descripcion">'.$row['value3'].' </p>
</td>
<td> '.$row['value4'].' </td>
<td> '. $row['value5'].' </td>
<td><p class="text-center">'. $row['value6'] .'% </p></td>
<td> '.$row['value7'].' </td>
<td> '.$row['value8'].' </td>
<td> '.$row['value9'].' </td>
<td> '.$row['value10'].'</td>
<td> '.$row['value11'].' </td>
<td> '.$row['value12'].' </td>
<td> '. $row['value13'].' </td>
<td>
<button class="center-block btn btn-acept"><i class="fa fa-check" aria-hidden="true"></i>
</button>
</td>
<td>
<button onclick="values('. $row['id']. ')" class="center-block btn btn-editar launch-modal" data-modal-id="modal-edit"><i
class="fa fa-pencil" aria-hidden="true"></i>
</button>
</td>
</tr>';
}
echo '</tbody>
</table>
</div>';
}
You add quotes, and escape them 您添加引号,然后将其转义
<button onclick="values(\''. $row['id']. '\')" class="center-block
As you're using doublequotes for the attribute, you'll need singlequotes for the argument, but you're already using singlequotes in PHP, so you have to escape them so they aren't parsed by PHP. 当您在属性中使用双引号时,您将需要在参数中使用单引号,但是您已经在PHP中使用了单引号,因此必须对其进行转义,以免被PHP解析。
Of course, the better option would be to not use inline javascript at all, but addEventListener
. 当然,更好的选择是根本不使用嵌入式javascript,而是使用addEventListener
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.