根据数据库值创建一个onclick表

时间:2013-03-22 16:26:38

标签: php jquery html-table onclick

我有点问题。我正在使用本教程

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”页面的<tr>部分创建一个onlick但它不起作用或者因为它不会或我做错了,后来我在想。

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

-------------------- EDIT --------------------------

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

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

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

1 个答案:

答案 0 :(得分: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>