0

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

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,我尝试<tr>在“search-data.php”页面的一部分创建一个 onlick,但它没有工作,因为它不会或者我做到了错了,我想的越晚。

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

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

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

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

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

4

1 回答 1

0

由于您已经在使用 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>
于 2013-03-22T16:34:39.533 回答