更新ajax表单提交的mysqli查询

时间:2018-01-08 08:34:47

标签: php jquery mysql ajax mysqli

我有一个动态的php页面,显示数据库的所有结果,我想创建一个搜索栏,用于更新用户输入的每个字符。 我的问题是我不能让查询更新!他总是没有显示任何结果。首先,我在与表单相同的页面中进行了查询,但是这不起作用,因为当我在参数中使用该url时,ajax将运行页面中的所有代码,所以我将所有内容分开,但它仍然无效。任何sugestions?

PHP页面 - 带搜索栏的部分 - > categorias.php:

<div class="container">
<h2 class="title">Empresas De Confianza</h2>
<div class="col-lg-12">
    <div class="col-lg-6 col-centered">
        <form method="POST" action="buscador.php" id="search">
            <input class="search-bar" type="text" name="filterName" id="filterName">
            <button type="submit" hidden></button>
            <?php
            $msqliquery = "SELECT * FROM empresa WHERE categorias_id = $id AND menulogo IS NOT NULL";
            $array = $connection->query($msqliquery);
            ?>
        </form>
    </div>
</div>


<?php
echo '<div class="row index-margin" id="results">';

if (mysqli_num_rows($array) == 0) {
    echo "<h2 class='text-center'>No hemos encontrado ningún resultado con esa busqueda!</h2><ul class='text-center'><li class='list-categorias'><a class='fa-categorias fa fa-refresh' href='categorias.php?id=$id'></a></li></ul>";
};
while ($field = mysqli_fetch_array($array)) {
    ?>
    <div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 parent size">
        <a style="background-color:<?php echo $field['color'] ?>"
           href="profile.php?id=<?php echo $field['id'] ?>">
            <img src="/test<?php echo $field['menulogo'] ?>" class="logo img-grayscale">
            <div class="hover-item"><h5 class="label-profile"><?php echo $field['nombre'] ?></h5></div>
        </a>
    </div>
<?php }
echo '</div>' ?>

“ 表格/搜索栏处理 - &gt; buscador.php:

<?php
require 'config.php';
$connection = new mysqli($servername, $username, $password, $db);
session_start();
$id = $_SESSION['id'];

$filterName = $_POST['filterName'];
$msqliquery = "SELECT * FROM empresa WHERE categorias_id = $id AND menulogo 
IS NOT NULL AND nombre COLLATE UTF8_GENERAL_CI LIKE '%$filterName%'";
$array = $connection->query($msqliquery);

每个字符和ajax表单的更新功能提交:

$(document).ready(function () {

    var timeoutID = null;

    $('#filterName').keyup(function (e) {
        clearTimeout(timeoutID);
        timeoutID = setTimeout(searchEmpresa.bind(undefined, e.target.value), 500);
    });

    function searchEmpresa(str) {
        console.log('search: ' + str);
        $("#search").submit(function (event) {
            $.ajax({
                method: "POST",
                url: 'buscador.php',
                data: $("#filterName").val(),
                success: function (data) {
                    console.log(data);
                    $("#results").load("categorias.php #results > *");
                }
            });
            event.preventDefault();
        });
    }
})

1 个答案:

答案 0 :(得分:0)

您必须将数据作为具有键值对的对象发送

data: {filterName:$("#filterName").val()},

此外,你必须更正方法:&#39;发布&#39;,所以你应该有发布方法而不是发布类型 希望它会对你有所帮助。

将此代码放入buscador.php

<?php

if (mysqli_num_rows($array) == 0) {
echo "<h2 class='text-center'>No hemos encontrado ningún resultado con esa 
busqueda!</h2><ul class='text-center'><li class='list-categorias'><a 
class='fa-categorias fa fa-refresh' href='categorias.php?id=$id'></a></li>
</ul>";
};
 while ($field = mysqli_fetch_array($array)) {
 ?>
  <div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 parent size">
    <a style="background-color:<?php echo $field['color'] ?>"
       href="profile.php?id=<?php echo $field['id'] ?>">
        <img src="/test<?php echo $field['menulogo'] ?>" class="logo img-
  grayscale">
        <div class="hover-item"><h5 class="label-profile"><?php echo 
 $field['nombre'] ?></h5></div>
    </a>
  </div>
 <?php }
echo '</div>' ?>

并将其附加到带有id结果的div

$(&#39;#结果&#39)附加(数据);