Galgu Posted May 13, 2020 Share Posted May 13, 2020 Hello everyone, I begin in everything web related but I have been programming for years. I tried to code something simple : small Mysql DB (works fine) and to begin a search bar to browse data. I adapted a code that I understood provided here : https://www.cloudways.com/blog/live-search-php-mysql-ajax/. Base principle is simple : as you type in your query, it will pass the text to script.js that will forward this request to ajax.php file. In the ajax.php, a javascript function named “fill()” will pass the fetched results. This function will also display the result(s) into “display” div in the “search.php” file. The problem is that when I type anything it displays, below the search bar, at the moment I type a character: Quote '; //Fetching result from database. while ($Result = MySQLi_fetch_array($ExecQuery)) { ?> ")'> instead of the actual answer from my database (no error in the browser console). I tested the SQL query + the user I provide and everything seems fine. Any clue what could be the root cause ? I strongly suspect a mistake in the code as I already corrected one (script.js instead of scripts.js) but I really cannot figure out where. Thanks in advance, problematic code (ajax.php): <?php //Including Database configuration file. include "db.php"; //Getting value of "search" variable from "script.js". if (isset($_POST['search'])) { //Search box value assigning to $Name variable. $Name = $_POST['search']; //Search query. $Query = "SELECT Name FROM search WHERE Name LIKE '%$Name%' LIMIT 5"; //Query execution $ExecQuery = MySQLi_query($con, $Query); //Creating unordered list to display result. echo ' <ul> '; //Fetching result from database. while ($Result = MySQLi_fetch_array($ExecQuery)) { ?> <!-- Creating unordered list items. Calling javascript function named as "fill" found in "script.js" file. By passing fetched result as parameter. --> <li onclick='fill("<?php echo $Result['Name']; ?>")'> <a> <!-- Assigning searched result in "Search box" in "search.php" file. --> <?php echo $Result['Name']; ?> </li></a> <!-- Below php code is just for closing parenthesis. Don't be confused. --> <?php }} ?> </ul> Quote Link to comment Share on other sites More sharing options...
gw1500se Posted May 13, 2020 Share Posted May 13, 2020 What is 'MySQLi_query'? Did you write your own function? PHP is case sensitive. I am guessing you meant 'mysqli_query'. Quote Link to comment Share on other sites More sharing options...
Barand Posted May 13, 2020 Share Posted May 13, 2020 An AJAX script returns everything in it's response that a normal script would send to the browser. Try calling ajax.php as a non-ajax request (in you browser as you would any other php script). What you see is what it would return in its response. 4 hours ago, gw1500se said: PHP is case sensitive Not when it comes to function names. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.