Internal search with HTML [closed]

-1

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

    
asked by anonymous 13.09.2017 / 02:48

1 answer

1

Generally an application like this has a very well defined database structure, with relationships between tables if necessary and a programming language on the server side controlling everything. If your application did not show need all that, okay, but making things dynamic on a static content site is hard work and does not always produce satisfying results. That being said, I recommend you to study a programming language and database. The PHP + MySQL combination is very responsive 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 was built on static files, directly in HTML, I believe the amount of content should be small - if it is not, you should learn the programming language urgently. And since you just want to do a search and redirect, you can store the data for all movies in a structure in JavaScript and use it in searches.

Suppose there are 3 movies, so 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 must be updated manually. So by typing something into the search field, you could perform a fuzzy search on movie titles by returning 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>

Notice that when you start typing a title in the field a list of suggestions with the links for each movie is created just below. If this result is already good for you, you may already find that you will need to study JavaScript.

    
13.09.2017 / 04:05