简体   繁体   English

获取我的数据库值作为字符串,以将其传递给我的javascript函数PHP

[英]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.

相关问题 使用$ .get()javascript将值传递给我的控制器方法 - Pass a value to my controller with $.get() javascript to my controller method 我怎样才能将值从javascript传递给我的php函数codeigniter - how can i pass value from javascript to my php function codeigniter 如何让我的 javascript 倒计时 function 使用我的数据库值 - How to get my javascript countdown function to work with my database values 无法从我的javascript函数中获取值 - Unable to get the value from my javascript function php:如何将数据库值传递到javascript / jquery函数 - php : how to pass database value into a javascript/jquery function 我的 function 无法传递它的参数值 javascript - my function can't pass it's parameter value javascript HTML从textarea获取输入并将其传递给我的Javascript函数 - HTML get input from textarea and pass it to my Javascript function How can I pass the string value generated from "ex.Message" from my C# function to my Javascript function Sweet Alert, to display the error Message - How can I pass the string value generated from "ex.Message" from my C# function to my Javascript function Sweet Alert, to display the error Message 如何从我的代码后面传递一个非常长的字符串到我的javascript函数? - How do I pass a very long string to my javascript function from my code behind? 我的 JavaScript 函数没有在我的 php 代码中运行 - my JavaScript function is not running in my php code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM