【问题标题】:Creating a table onclick based on database value根据数据库值创建表 onclick
【发布时间】:2017-08-14 00:08:29
【问题描述】:

我有一个小问题。我正在使用本教程

http://www.codemashups.com/dynamic-search-filtering-using-jquery-and-php/

它允许我搜索我的数据库并在表格中返回结果。 这是页面“search-data.php”中代码的基础

    if ($conn) {

    /* Fetch the users input from the database and put it into a
     valuable $fetch for output to our table. */
    $fetch = mysql_query("SELECT * FROM clients 
                         WHERE client_name LIKE '%{$param}%' OR client_email REGEXP '^$param'  OR client_address LIKE '%{$param}%' OR client_postcode LIKE '%{$param}%' OR client_phone REGEXP '^$param'");

    /*
       Retrieve results of the query to and build the table.
       We are looping through the $fetch array and populating
       the table rows based on the users input.
     */
    while ( $row = mysql_fetch_object( $fetch ) ) {
        $sResults .= '<tr id="'. $row->id . '" >';
        $sResults .= '<td>' . $row->client_name . '</td>';
        $sResults .= '<td>' . $row->client_email . '</td>';
        $sResults .= '<td>' . $row->client_address . '</td>';
        $sResults .= '<td>' . $row->client_postcode . '</td>';
        $sResults .= '<td>' . $row->client_phone . '</td>
        </tr>';
    }

}

/* Free connection resources. */
mysql_close($conn);

/* Toss back the results to populate the table. */
echo $sResults;

一切正常,我可以使用我的表格进行搜索,它会显示我想要的信息,你问得真好!那你想要什么?

好吧,我的表是存储客户信息的,在我之前做过的不可搜索的表中,您从列表中单击所需的客户,然后转到他们所有信息的页面(列表中的信息是仅限于姓名和地址,因为整页都有电话、邮政编码等),为此我使用了 onclick。

所以这是我的新可搜索表的表:

            <table>
                <tr>
                    <th class="ui-state-default">Name</th>
                    <th class="ui-state-default">Email</th>
                    <th class="ui-state-default">Address</th>
                    <th class="ui-state-default">Postcode</th>
                    <th class="ui-state-default">Phone</th>
                </tr>
            <tbody id="results" onclick="location='../clients.php?action=viewClient&amp;clientId=<?php echo  ?>'" ></tbody>
        </table>

就像我说的那样效果很好,并且所有客户端信息都出现了,但是我需要在 onclick 行的回声之间出现“id”(我想如果它显示就不需要回声正确的 ID)。

onclick="location='../clients.php?action=viewClient&amp;clientId=<?php echo  ?>

所以任何人都可以看到一个简单的方法来获取关于 id 的信息到 onclick,我尝试在“search-data.php”页面的&lt;tr&gt; 部分创建一个 onlick,但它也没有工作,因为不会的,不然我做错了,以后我在想。

无论如何,任何帮助都会很棒。

--------------------编辑--------------

我发现我可以创建一个有效的链接。我可以像这样在结果上创建链接:

 $sResults .= '<td><a href="../clients.php?action=viewClient&amp;clientId='. $row->id .'">'. $row->client_name .'</a></td>';

这会创建一个工作链接,但是当我尝试为 onclick 做类似的事情时,它甚至不会去任何地方,更不用说正确的地方了,好像它不喜欢那样做,如果这对任何人都有帮助进一步解决 onclick 问题会很棒。

【问题讨论】:

  • 您是否创建了一个生成表格的 php 页面?
  • 你应该只使用真实的链接...
  • 我不使用真正的链接,我有一个名为 viewClient 的页面,它是根据 id 动态填充的,所以如果链接中的 id 为 8 将显示不同用户的详细信息,而不是1

标签: php jquery html-table onclick


【解决方案1】:

由于您已经在使用 jQuery,请删除表格上的 onclick 并添加以下 javascript:

根据本教程,您应该需要将搜索页面的 head 部分更新为此,并且您无需更新任何其他代码即可使其正常工作。

<head>
    <title>jQuery Search Autocomplete</title>

    <style type="text/css" title="currentStyle">
        @import "css/grid_sytles.css";
        @import "css/themes/smoothness/jquery-ui-1.8.4.custom.css";
    </style>

    <!-- jQuery libs -->
    <script  type="text/javascript" src="js/jquery-1.6.1.min.js"></script>
    <script  type="text/javascript" src="js/jquery-ui-1.7.custom.min.js"></script>

    <script  type="text/javascript" src="js/jquery-search.js"></script>

    <script type="text/javascript">
        (function($) {
            $('td').click(function() {
                var id = $(this).parent().attr('id');
                window.location = '../clients.php?action=viewClient&clientId=' + id;
            }
        }(jQuery));
    </script>
</head>

【讨论】:

  • 我不太了解Jquery,仍然不确定如何从“search-data.php”页面获取id。
  • 第 3 行是处理检索 ID 的内容。所以基本上,如果在表格中单击任何单元格,它会上升到作为 TR 标签的父级并查看其上的 id 属性并将 ID 属性存储到变量 id 中,然后将 id 附加到末尾生成窗口位置的字符串。
  • 感谢您的回复。抱歉这么晚才回复。不幸的是,我无法让它工作,当我单击结果行时,它没有链接到任何地方,好像它没有识别 javascript 中的 onclick 功能:(
猜你喜欢
  • 1970-01-01
  • 2019-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 2016-02-12
  • 1970-01-01
相关资源
最近更新 更多