Internal search with HTML

Asked

Viewed 4,482 times

-1

Good night people! I’m creating a movie site and I wanted to know how to do an internal search system on it, like, everything is already created, the input and the button, now just have to make it work! I want that at the same time I type in the bar, it suggests some titles and when clicking redirect to a specific page. I’m a beginner in all of this, I know little about programming in general, and I need help! I thought about creating an HTML page with a "list" of movies, and this list would be used in the suggestions. Thank you :)

  • Welcome, to get an answer that solves your question/problem, please read How to ask a good question? and How to create a Minimum, Complete and Verifiable example

  • 1

    For this you will need a programming language. Depending on the structure of your application this language should be executed on the Javascript-aided server side or only this second. In general, your question is too wide and cannot be answered. How are the film data stored? Are you using any language on the server? What? What database? Do you know about Javascript?

  • I have no server yet, the site is all in html and css. each movie is a page, and to redirect use a figure with anchor. Basically this. I have no knowledge in JS. And I have no database

  • With pure HTML you won’t be able to do anything

  • And with JS, what would a base look like?

1 answer

1


Usually such an application has a very well defined database structure, with relationships between tables, if necessary, and a server-side programming language controlling everything. If your application hasn’t shown a need for it all, okay, but making things dynamic on a static content site is hard work and doesn’t always produce satisfactory results. That said, I recommend you to study a programming language and database. The combination PHP + Mysql is very receptive to beginners.

Only with Javascript is it possible to create something, but as I said, the result is not always so satisfactory. Given that your entire site has been built into static files, directly into HTML, I believe that the amount of content should be small - if not, you should learn the programming language urgently. And since you just want to do a search and redirect, you can store the data of all the movies in a structure in Javascript and use it in searches.

Let’s assume there are three movies, then you could do something like:

const movies = [
  {title: "Alice no País das Maravilhas", url: "alice.html"},
  {title: "Harry Potter e a Pedra Filosofal", url: "harry.html"},
  {title: "Karate Kid", url: "karate.html"}
];

Whenever a new movie is added to the site, this structure should be updated manually. So, by typing something into the search field, you could perform a search Fuzzy in the titles of the films returning the related titles.

const movies = [{
    title: "Alice no País das Maravilhas",
    url: "alice.html"
  },
  {
    title: "Harry Potter e a Pedra Filosofal",
    url: "harry.html"
  },
  {
    title: "Karate Kid",
    url: "karate.html"
  }
];

// Estes elementos servirão para a manipulação do DOM: elementos HTML
const title = document.getElementById("title");
const list = document.getElementById("movies");

// Aqui atribuímos uma função ao evento `keyup` do campo no HTML
// Isto é, sempre que uma tecla for pressionada, a função será executada:
title.addEventListener("keyup", function(event) {

  // Filtramos a lista de filmes com base no texto digitado:
  const matchs = movies.filter(value => {

    // Se o texto digitado for encontrado no título, retorna o registro:
    return value.title.indexOf(this.value) !== -1;

  });
   
  // Exibe no HTML a lista de filmes do resultado do filtro anterior:
  list.innerHTML = "";
  for (let movie of matchs) {
    list.innerHTML += "<li><a href='"+movie.url+"'>"+movie.title+"</a></li>";
  }
});
<input type="text" id="title" placeholder="Digite o título do filme">

<ul id="movies"></ul>

Note that when you start typing a title in the field a list of suggestions with the links to each movie is created right below. If this result is already satisfactory for you, you can already conclude that you need to study Javascript.

  • Thank you very much! This is already of good size, I want something simple even, and gradually I will study and perfecting. But now I was in doubt if I focus on PHP + Mysql or if I learn JS.

  • In the end, both. They are not unique. It depends on whether you want to act more on the client side, studying JS, or on the server side, studying PHP.

  • Right! Thank you very much. Solved my problem

  • Hello guys, I liked this code and as it would be if I had two search fields, title and url for example and would like to do the search that contains the values typed, the user can type one or the other or both.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.