【问题标题】:Bootstrap modal doesn't show database search results引导模式不显示数据库搜索结果
【发布时间】:2021-07-12 11:36:51
【问题描述】:

我已经在我的网站中实现了this 代码。此代码可帮助我在同一页面上以 Bootstrap 模式获取搜索结果,而无需访问浏览器中的其他页面。

一切正常:模式出现,关闭,除了我在 search.php 中的 PHP 代码。我运行 console.log 来查看我从 search.php 获得的数据,我只获得 HTML 代码,PHP 代码被排除在外。我在想,也许是我写错了代码,或者括号位置不好,但我已经尝试了任何想到的解决方案。

index.php

$("#searchForm").submit(function(e) {
// Avoid to execute the actual submit of the form.
e.preventDefault();

var searchForm = $(this);
var searchData = searchForm.serialize(); // Serialize the form's elements.
var searchURL = "map/search.php";

$.ajax({
    type: "POST",
    url: searchURL,
    cache: false,
    data: searchData,
    success: function(data) {
        $("#searchModal .modal-content").html(data);
        $("#searchModal").modal('show');
        console.log(data);
    },
    error: function(jqXHR, textStatus, errorThrown) {
        console.log(textStatus, errorThrown);
    }
    });
});

console.log / alert()

<!-- search_modal_content -->
<div class = "modal-content">
    <div class = "modal-header">
        <h4 class = "modal-title">Rezultate cautare</h4>
    </div>

    <div class = "modal-body">
            <div class = "modal-footer">
        <button type = "button" class = "btn btn-danger" data-dismiss = "modal">Inchide</button>
    </div>
</div>


<script type = "text/javascript">
    $('.close, .btn-danger').click(function() {
        $("#searchModal" ).modal('hide');
    });
</script>

search.php

<?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    require_once __DIR__ . '../../config/config.inc.php';

?>

<!-- search_modal_content -->
<div class = "modal-content">
    <div class = "modal-header">
        <h4 class = "modal-title">Rezultate cautare</h4>
    </div>

    <div class = "modal-body">
        <?php 
            if(isset($_POST['submit'])) {
                $searchValue = $_POST['search'];
        
                $search_result = mysqli_query($link, "SELECT * FROM clubajj WHERE Nume or ADRESA LIKE '%$searchValue%'");
                $count = mysqli_num_rows($search_result);

                if($count < 1) {
                    echo '<p class = "text-align-center font-weight-bold">Nu s-a gasit niciun rezultat care sa corespunda cu cautarea dumneavoastra.</p>';
                } else { 
        ?>
        <table class = "table table-responsive table-inverse table-hover table-striped">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Nume</th>
                    <th>Judet</th>
                    <th>Adresa</th>
                </tr>
            </thead>
            <?php while($row = mysqli_fetch_assoc($search_result)) {
                ?>

            <tbody>
                <tr>
                    <td><?php echo $row['ID']; ?></td>
                    <td><?php echo $row['Nume']; ?></td>
                    <td><?php echo $row['Judet']; ?></td>
                    <td><?php echo $row['Adresa']; ?></td>
                </tr>
            </tbody>
            <?php } ?>
        </table>
        <?php } ?>
    </div>
    <?php } mysqli_close($link); ?>
    <div class = "modal-footer">
        <button type = "button" class = "btn btn-danger" data-dismiss = "modal">Inchide</button>
    </div>
</div>


<script type = "text/javascript">
    $('.close, .btn-danger').click(function() {
        $("#searchModal" ).modal('hide');
    });

<form class = "d-flex" id = "searchForm" method = "POST">
                        <input class = "form-control me-2" type = "text" placeholder = "Cautare" aria-label = "Cautare" name = "search">
                        <button class = "btn btn-outline-success" type = "submit" name = "submit" data-toggle = "modal" data-target = "#searchModal">Cauta</button>
                    </form>

【问题讨论】:

  • 最可能的原因是 if(isset($_POST['submit'])) { 返回 false,因此该块中的其余内容没有输出。不幸的是,您没有向我们展示您的表单是什么样子,或者searchData 的内容是什么,所以我们无法确定是否发生了这种情况。您是否尝试过自己调试?
  • P.S.不仅仅是 PHP 生成的内容被排除在外——如果你注意到,硬编码的表头部分也丢失了——这是另一个线索,它根本没有进入 if 块。
  • 我已经添加了表单,正如你所说,@ADyson。
  • 谢谢。这可能是因为您的submit 按钮没有value,因此当接收到数据时,服务器上可能没有设置任何值。您可以通过查看用于 AJAX 请求的浏览器的网络工具并查看发送的表单数据来验证发送的内容(这就是我在原始评论中询问您是否进行任何调试的意思)。
  • @ADyson,我看过你告诉我的地方,这就是我得到的:i.imgur.com/KLxaCjN.png。我也一直在另一个页面上使用 AJAX 请求,这是我从该页面获得的数据:i.imgur.com/CXfsKIj.png。在此搜索表单的旧版本中(当 search_results 显示在单独的页面上时),submit 按钮没有任何价值并且可以正常工作。

标签: javascript php


【解决方案1】:

您需要将 search.php 中的所有 html 代码移动到 index.php。仅您的搜索控制器响应 json 数据。在下面的 js 请求中,当从搜索结果中获取响应数据时,将 json 渲染为 html。

$.ajax({
    type: "POST",
    url: searchURL,
    cache: false,
    data: searchData,
    success: function(data) {
        $("#searchModal .modal-content .table tbody").html("");
        for(let i = 0; i < data.length; i++){
           //insert each table row here:  
           //$("#searchModal .modal-content .table tbody").append(".....");
           //your data row is: data[i];
        }
    },
    error: function(jqXHR, textStatus, errorThrown) {
        console.log(textStatus, errorThrown);
    }
    });
});

希望这个解决方案可以帮助到你。

【讨论】:

  • 不,没有必要这样做。 AJAX 请求可以返回 HTML。返回 JSON 只是一种选择,它不是强制性的。尝试提出解决实际问题的建议,而不是不必要的重写。
猜你喜欢
  • 2016-09-03
  • 1970-01-01
  • 2018-01-15
  • 2015-08-12
  • 1970-01-01
  • 2021-02-23
  • 2017-11-23
  • 1970-01-01
  • 2019-03-17
相关资源
最近更新 更多