简体   繁体   English

如何使我的搜索栏工作

[英]How to get my search bar working

I'm trying to finalise a search bar for my CMS website, 我正在尝试为CMS网站确定搜索栏,

I'm nearly there just my php I think is the main issue, 我几乎只有我认为是主要问题的php,

I have search input 我有搜索输入

<div class="search">
  <form method="post" action="search.php" class="search" id="search">
    <input id='searchBar' class='search' placeholder="Search for a product" type='text'/>                 
   </form>
</div>

then I have some java script to support on key presses to hopefully filter down items that are currently being displayed on the screen, 然后我有一些按键可以支持的Java脚本,希望可以过滤掉当前显示在屏幕上的项目,

 function setSearchListener(){
 var searchBar = document.getElementById('searchBar');
 searchBar.addEventListener('keyup', function(){
 searchBar.addEventListener('focus', runSearchBar(this.value));
 });
}

 function runSearchBar(str){
var oldTarget = document.getElementById('productscontainer');
if(str.length == 0){
console.log('hello');
oldTarget.style.display = 'block';
}
else{
oldTarget.style.display = 'none';
}
var newTarget = document.getElementById('searchTarget');
fetch(newTarget);
}





function fetch(target) {
var xhr, target;
// create a request object
xhr = new XMLHttpRequest();

changeListener = function () {
    if (xhr.readyState === 4 && xhr.status === 200) {

        target.innerHTML = xhr.responseText;
    } else {
        target.innerHTML = "<p>Something went wrong.</p>";
    }
};

xhr.open("GET", "sql/search.php", true);
xhr.onreadystatechange = changeListener;
xhr.send();
console.log(name);
console.log('finding');

};
window.addEventListener('load', setSearchListener());

The javascript side seems to be working all good for me, however im unsure how to set up the php with database interaction side: javascript方面似乎对我来说都很好,但是我不确定如何与数据库交互方面设置php:

here is my search.php 这是我的search.php

<?php
include("../dbase/config_database.php");    

    $query = "SELECT * FROM products WHERE id LIKE '%".$s."%' OR name LIKE '%".$s."%' OR description LIKE '%".$s."%' ";
    $result = mysqli_query($mysqli, $query);
    $rows = mysqli_fetch_array($result);
    $response = array(
        'id' => $rows ['id'],
        'name' => $rows ['name'],
        'description' => $rows ['description'],
        'price' => $rows ['price'],
        'quantity' => $rows ['quantity']
    );
    echo json_encode($response);
    }
?>

I basically have a page with products displayed, what I would like is when you use the search bar on each keyup say "iphone" it will only display the iphone product(s) on the page and the other products wont be displayed unless the search bar keystrokes are removed. 我基本上有一个显示产品的页面,我想要的是当您在每个按键上使用搜索栏说“ iphone”时,它将仅在该页面上显示iphone产品,除非搜索,否则其他产品将不会显示。条键被删除。

I see a bug in the JavaScript: 我在JavaScript中看到一个错误:

window.addEventListener('load', setSearchListener());

should be 应该

window.addEventListener('load', setSearchListener);

and this is also wrong 这也是错误的

searchBar.addEventListener('keyup', function(){
    searchBar.addEventListener('focus', runSearchBar(this.value));
});

Every time a key up event is firing you are attaching another event. 每次触发按键事件时,您都将附加另一个事件。 Another issue is you are not also attaching the function, but calling the function. 另一个问题是您还没有附加函数,而是调用了函数。

Your code is a mess. 您的代码一团糟。 This row for example: 该行例如:

xhr.open("GET", "sql/search.php", true);

should be 应该

xhr.open("GET", "sql/search.php?searchstring=" + encodeURIComponent(document.getElementById("searchbar").value), true);

which means, right now you are not sending the search string to search.php. 这意味着,现在您没有将搜索字符串发送到search.php。

And in search.php, you're not trying to read the data. 在search.php中,您并不是要读取数据。 To be able to use $s as the search string you need to do $s = $_GET['searchstring']; 为了能够将$s用作搜索字符串,您需要执行$s = $_GET['searchstring']; .

I recommend you to study a Javascript reference of choice and the PHP documentation. 我建议您研究选择的Javascript参考和PHP文档。 And promise me to never put user submitted data into an mysqli_query() call without using mysqli_real_escape_string() on it. 并保证我永远不要在不使用mysqli_real_escape_string()情况下将用户提交的数据放入mysqli_query()调用中。 It is basic security. 这是基本的安全性。

It is too many errors for one SO question. 一个SO问题有太多错误。 Split it up or go study. 拆分或去学习。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM