Form action without appearing name

Asked

Viewed 226 times

0

  • You have to do it with javascript, because the Submit of the kind get always assemble the fields as well as parameters in the url (name=value)

  • Wouldn’t it be the case to send a form method="post" and simply redirect on the server side? So it would be compatible with any client, even the older ones (and the user wouldn’t even see the target URL in the browser)

2 answers

0

I did this simple snippet and, although functional, is a gambiarra. There must be appropriate methods.

const form = document.forms[0]; // Cria referência para o formulário

function submit(event){
    event.preventDefault(); // Previne o comportamento padrão do botão submit

    const inputValue = form.busca.value; // Obtém o valor do campo de texto

    location.href = "http://google.com/" + inputValue; // Concatena a URL do Google com o valor do campo de texto e redireciona a página
}

form.submit.addEventListener("click", submit); // Espera o usuário clicar no botão submit, para executar a função submit();
<form> <!-- Retirei o atributo action -->
    <input type="text" name="busca">
    <br>
    <input name="submit" type="submit" value="Submit"> <!-- Adicionei o atributo name -->
</form>

0

In a simpler way:

Place a function that redirects to the concatenated URL with the input value

SCRIPT

function myFunction(){
    //o valor da variavel parametro é o valor do input de id=busca
    var parametro = document.getElementById("busca").value;  
    //redireciona para o url indicado concatenado com o valor da variavel parametro
    window.location.href = "http://google.com/" + parametro;

}

HTML No need for tag form

  <input type="text" name="busca" id="busca">
  <br>
  <button type="button" onclick="myFunction()">Submit</button>

See working

function myFunction(){
var parametro = document.getElementById("busca").value;  
console.log ("http://google.com/" + parametro);
}
<input type="text" name="busca" id="busca">
<br>
<button type="button" onclick="myFunction()">Submit</button>

Browser other questions tagged

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