dpleylxzx47207117 2015-01-15 19:46
浏览 33
已采纳

如何从我的数据库填充的表中搜索

I have a MySQL DB table as users.

I am populating a html table using php and ajax.(working fine)

but I want to search for specific data, written some code also, but not getting what is my mistake.

my index.html

<body>
    <div class="container">
                <h3>Product List</h3>
            <div id="users-grid">               
            </div>
        </div>
</body>
<script>
    function getresult(url) {    
    $.ajax({
        url: url,
        type: "POST",
        success: function(data){ $("#users-grid").html(data);}
       });
    }
    getresult("getresult.php");
</script>

my getresult.php(used to populate the #users-grid )

<?php 
    include 'database.php';
    $pdo = Database::connect();
    $product = "";
    $queryCondition = "";
?>
<form name="frmSearch" method="post" action="index.php">

    <div class="search-box">
    <p>
    <input type="text" placeholder="Product Name" name="product"  value="<?php echo $product; ?>"   />
    <input type="button" name="go" class="btnSearch" value="Search" onclick="getresult('getresult.php')">
    <input type="reset" class="btnSearch" value="Reset" onclick="window.location='index.php'"></p>
    </div>

    <table class="table table-striped table-bordered" class="tablesorter">
        <thead>
            <tr>
            <th>Date</th>
            <th>Product</th>
            <th>Quantity</th>
            <th>Gross Price</th>
            <th>Profit</th>
            </tr>
        </thead>
        <tbody>
            <?php
                $queryCondition = "";
                if(!empty($_POST["product"])) {
                    $queryCondition .= " WHERE product LIKE '" . $_POST["product"] . "%'";
                }
                $orderby = " ORDER BY id desc"; 
                $sql = "SELECT * FROM users " . $queryCondition . $orderby;
                foreach ($pdo->query($sql) as $row) {
                    echo '<tr>';
                    echo '<td>'. $row['date'] . '</td>';
                    echo '<td>'. $row['product'] . '</td>';
                    echo '<td>'. $row['quantity'] . '</td>';
                    echo '<td>'. $row['grossprice'] . '</td>';
                    echo '<td>'. $row['profit'] . '</td>';
                    echo '</tr>';
                }
                Database::disconnect();
            ?>
        </tbody>
    </table>
</form>

Thank you

  • 写回答

1条回答 默认 最新

  • dongye9820 2015-01-15 19:55
    关注

    this code will search anything start with your search query

    $queryCondition .= " WHERE product LIKE '" . $_POST["product"] . "%'";
    

    i think you should change it to this for haveing full search

    $queryCondition .= " WHERE product LIKE '%" . $_POST["product"] . "%'";
    

    when you put onclick on your submit button it will fire only when user click on the button but when user put something on text and press enter it will submit your form so you have to change your code to this

    <form name="frmSearch" method="post" onsubmit="return getresult('getresult.php')">
        <div class="search-box">
            <input type="text" placeholder="Product Name" name="product" value="<?php if(isset($_POST["product"])) echo $_POST["product"]; ?>"   />
            <input type="button" name="go" class="btnSearch" value="Search">
            <input type="reset" class="btnSearch" value="Reset" onclick="window.location='index.php'">
        </div>
    </form>
    

    i changed input value to show your search query. and the return part is for stop page reload

    and to send POST data you have to change your ajax code to this

    function getresult(url) {   
        $.ajax({
            url: url,
            type: "POST",
            data: {product: $('input[name=product]').val()},
            success: function(data){ $("#users-grid").html(data);}
       });
       return false
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大