提问人:jorge garcia 提问时间:11/17/2023 最后编辑:ADysonjorge garcia 更新时间:11/17/2023 访问量:45
有谁知道为什么 html 在我搜索时会重复?
Does anyone know why the html duplicates when im searching?
问:
我正在做一个搜索页面,我在其中放置一个输入并根据输入中的数据,它在数据库中搜索类似的文章并显示在框中。但是当我搜索某些内容时,页面中会出现另一个标题和输入。 重复的标头和输入
我试着问chatgpt,但它帮不了我。我也搜索过,但找不到类似的东西。
<?php
session_start();
if ($_SESSION["user"] == "" || $_SESSION["premium"] == "") {
header("Location: index.php");
exit();
}
$valorInput = isset($_GET["valor"]) ? $_GET["valor"] : "";
include "dbconnect.php";
$resultados = array();
if (!empty($valorInput)) {
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$stmt = $conn->prepare("SELECT name, description, link, imagelink, price FROM Articulos WHERE description LIKE ?");
$param = "%$valorInput%";
$stmt->bind_param("s", $param);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$resultados[] = '<div class="template">
<img src="' . $row['imagelink'] . '" alt="' . $row['description'] . '">
<p>' . $row['name'] . '</p>
<p>' . $row['description'] . '</p>
<p>Precio: ' . $row['price'] . '</p>
</div>';
}
$stmt->close();
echo implode('', $resultados);
exit();
}
}
$conn->close();
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/inicio.css">
<title>Buscador de Productos</title>
</head>
<body>
<header>
<h1>Buscador de Productos</h1>
</header>
<div id="search-container">
<input type="text" id="search-box" oninput="actualizarVariable()" placeholder="Buscar productos...">
<div id="search-results" class="search-results-container"></div>
</div>
<div id="result-container" class="container">
<?php
for($i = 0; $i < 6; $i++) {
echo '<div class="template">
<img src="imagen1.jpg" alt="';
echo $valorInput;
echo '">
<p>Descripción del Producto 1</p>
</div>';
}
?>
</div>
<script>
function actualizarVariable() {
var valorInput = document.getElementById("search-box").value;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("result-container").innerHTML = this.responseText;
}
};
xhttp.open("GET", "inicio.php?valor=" + valorInput, true);
xhttp.send();
}
</script>
<script src="script.js"></script>
</body>
</html>
答: 暂无答案
评论
HTTP_X_REQUESTED_WITH
if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])