【问题标题】:Get my database value as a string to pass it to my javascript function PHP将我的数据库值作为字符串传递给我的 javascript 函数 PHP
【发布时间】:2016-10-01 01:41:30
【问题描述】:

我的数据库中有一个字符串值,即 id,我有一个带有 javascript 函数的按钮 onclick="values('argument')"。

我的问题是,当我试图点击那个按钮时,我遇到了这个错误:

(index):1 Uncaught ReferenceError: argument is not defined

我知道发生这种情况是因为参数没有被视为字符串。

如何将参数作为字符串传递?

所有这些都是包含在一个 php 函数中的表,它显示来自我的数据库的信息

这是我的完整代码。

    <?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>';
}

【问题讨论】:

  • 错误是javascript,你的javascript代码在哪里?

标签: javascript php function mysqli


【解决方案1】:

你添加引号,然后转义它们

<button onclick="values(\''. $row['id']. '\')" class="center-block

当您对属性使用双引号时,您需要为参数使用单引号,但您已经在 PHP 中使用单引号,因此您必须对它们进行转义,以免它们被 PHP 解析。

当然,更好的选择是根本不使用内联 javascript,而是使用addEventListener

【讨论】:

  • 但如果我使用 addEventListener 如何将 $row['id'] 参数传递给 javascript 函数?
  • 使用数据属性将数据放入元素中,并在单击按钮时获取该数据属性。
  • 你可以举个例子解释一下吗?
  • 我得到了这个错误 jquery.min.js:2 Uncaught TypeError: Cannot read property 'addEventListener' of null
  • 不确定为什么发布的代码不使用 jQuery 时会出现 jQuery 错误?无论如何,元素必须可用才能获取它们,请注意脚本放置在具有给定 ID 等的元素之后。
猜你喜欢
  • 2021-07-24
  • 2016-08-08
  • 1970-01-01
  • 2013-05-02
  • 2011-05-12
  • 2012-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多