0

I'm trying to create a live search bar where I type names and I'm getting results from my database.

Error of the image

This is my project structure:

enter image description here

The files I'm using are map.js where I have my ajax call. My two php files one for mysql connection and one to retrieve the data from the database. And last my map.hbs where is my search bar.

map.js

function fill(Value) {
  //Assigning value to "search" div in "map.hbs" file.
  $('#search').val(Value);
  //Hiding "display" div in "map.hbs file.
  $('#display').hide();
}

$(document).ready(function() {
  //On pressing a key on "Search box" in "map.hbs" file. This function will be called.
  $("#search").keyup(function() {
      //Assigning search box value to javascript variable named as "name".
      var name = $('#search').val();
      //Validating, if "name" is empty.
      if (name == "") {
          //Assigning empty value to "display" div in "map.hbs" file.
          $("#display").html("");
      }
      //If the name is not empty.
      else {
          //AJAX is called.
          $.ajax({
              //AJAX type is "Post".
              method: "POST",
              //Data will be sent to "ajax.php".
              url: "/php/loadData.php",
              
              //Data, that will be sent to "ajax.php".
              data: {
                  //Assigning value of "name" into "search" variable.
                  search: name
              },
              //If result found, this function will be called.
              success: function(html) {
                  //Assigning result to "display" div in "map.hbs" file.
                  $("#display").html(html).show();
              }
          });
      }
  });
});

db.php

<?php
$con = MySQLi_connect(
    "localhost", //Server host name.
    "root", //Database username.
    "", //Database password.
    "covid_database" //Database name or anything you would like to call it.
 );
 
//Check connection
if (MySQLi_connect_errno()) {
   echo "Failed to connect to MySQL: " . MySQLi_connect_error();
}
?>

loadData.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 pois WHERE name LIKE '%$Name%' LIMIT 5";
 
//Query execution
   $ExecQuery = MySQLi_query($con, $Query);
 
//Creating unordered list to display the result.
   echo '
<ul>
   ';
   //Fetching result from the database.
//    while ($Result = MySQLi_fetch_array($ExecQuery)) {
//        ?>
//    <!-- Creating unordered list items.
 
//         Calling javascript function named as "fill" found in "map.js" file.
 
//         By passing fetched result as a parameter. -->
 
//    <li onclick='fill("<?php echo $Result['Name']; ?>")'>
//    <a>
//    <!-- Assigning searched result in "Search box" in "map.hbs" file. -->
 
//        <?php echo $Result['Name']; ?>
//    </li></a>
//    <!-- Below php code is just for closing parenthesis. Don't be confused. -->
//    <?php
// }}
// ?>
// </ul>

?>

map.hbs

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

{{!-- Search Bar --}}
    <form class="form-inline my-2 my-lg-0">
        <input class="form-control mr-sm-2" type="text" id="search" placeholder="Search" aria-label="Search">
        <button class="btn btn-outline-success my-2 my-sm-0"  type="submit" oninput="triggerSearch()">Search</button>
        <div id="display">
        </div>
    </form>
    
    <script src="/map.js"></script>
2
  • What is your question? Commented May 8, 2022 at 21:08
  • Sorry for the late response. Im trying to rewrite the php program in javascript. My question is. How can i take my live searching text and create an ajax get where it is searching my database? What i'm currently trying is creating and ajax get where i send my search text to a route in my app.js and creating there a database query. Commented May 9, 2022 at 14:01

1 Answer 1

1

Your server, which you seem to have written in Node.js, does not recognise the URL you are requesting. This would be because you haven't written an endpoint handler for it.

Your options:

  • Write an endpoint handler in JS that runs the PHP (Node.js is not the ideal for this, but I believe it is possible).
  • Run a different server which has decent PHP support (such as Nginx or Apache HTTPD.
  • Rewrite the PHP program in JavaScript

I'd go for the latter myself, mixing server-side languages is usually more complication than it is worth.

Sign up to request clarification or add additional context in comments.

1 Comment

About the last one. I searched in google to make an ajax call without php, but cant find anything. Any tips for it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.