How to make a search for html

"Search for HTML elements in your code quickly with this easy example."

How to Make a Search in HTML

A search box provides users with a simple and easy-to-use way to find information on a website. Creating a search box using HTML is a straightforward process.

To start, you need to create an HTML form. The form will contain an input field for the user to enter their query, and a submit button for the user to submit their query. Here’s an example of the HTML code:


<form action="/search" method="get">
  <input type="text" name="q">
  <input type="submit" value="Search">
</form>

This code creates a form with an input field and a submit button. When the user submits the form, the “q” parameter will be sent with the query they entered in the input field.

The next step is to create a search page that will handle the search query. This page will need to process the query, search for the results, and display them to the user. Here’s an example of the code for the search page:


// Get the query from the URL
$query = $_GET['q'];

// Connect to the database
$db = mysqli_connect('localhost', 'user', 'password', 'database');

// Execute the query
$results = mysqli_query($db, "SELECT * FROM products WHERE name LIKE '%$query%'");

// Display the results
while ($row = mysqli_fetch_array($results)) {
  echo "<h3>{$row['name']}</h3>";
  echo "<p>{$row['description']}</p>";
}

This code connects to a database, executes a query, and displays the results. The query uses a “LIKE” statement to search for the query string in the “name” field of the “products” table. The results are then displayed to the user.

With these two pieces of code, you can create a simple search box that will allow users to search your website. You can modify the code to add features like pagination, sorting, and filtering, or to search other fields in the database.

h

Answers (0)