[英]update mysqli query on ajax form submit
I have a dynamicaly php page that displays all results from database and i want to create a search bar that updates at every character the user inputs. 我有一个动态的php页面,该页面显示数据库的所有结果,并且我想创建一个搜索栏,该栏会在用户输入的每个字符处进行更新。 My problem is that i can't make the query update!
我的问题是我无法进行查询更新! he always displays no results.
他总是不显示结果。 First I had the query in the same page as the form but that wouldnt work because the ajax would run all the code from the page when i used that url in the parameters so i splitted everything but it still doesnt work.
首先,我在与表单相同的页面中进行查询,但是那行不通,因为当我在参数中使用该URL时,ajax会运行该页面中的所有代码,因此我拆分了所有内容,但仍然无法正常工作。 Any sugestions?
有任何建议吗?
PHP page - part with search bar --> categorias.php: 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>' ?>
´ Form/search bar handling --> buscador.php: ´表单/搜索栏处理-> 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);
Function for update at every character and ajax form submit: 用于更新每个字符和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();
});
}
})
You have to send data as object with key value pairs 您必须将数据作为具有键值对的对象发送
data: {filterName:$("#filterName").val()},
Also you have to correct method: 'post', So you should have method of posting not type of posting Hope it will help you. 另外,您必须更正方法:“ post”,因此,您应该具有发布方法,而不是发布类型希望对您有所帮助。
put this code in your buscador.php 将此代码放在您的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>' ?>
and append it to your div with id result 并将其附加到具有ID结果的div中
$('#result').append(data); $('#result')。append(data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.