Get data from a form and create a url to which I redirect with javascript


I have a form and I would like it when I press a button to redirect me to a page with the values in the url, something like this:


"[email protected]".

This is my code:

<button id="send" onclick="prueba()">Enviar</button>

function prueba() {
    var id = getParameterByName('id');
    var email = document.getElementById("email").value;
    var ocupacion = document.getElementById("ocupacion").value;
    var dni = document.getElementById("dni").value;
    var edad = document.getElementById("edad").value;
    var estadoc = document.getElementById("estadoc").value;
    var distrito = document.getElementById("distrito").value;
    var nombrecon = document.getElementById("nombrecon").value;
    var emailcon = document.getElementById("emailcon").value;
    var ocupacioncon = document.getElementById("ocupacioncon").value;
    var url = "" + "?id=" + id + "&em=" + email + "&ocu=" + ocupacion + "&dni=" + dni + "&edad=" + edad + "&est=" + estadoc + "&dis=" + dis + "&nomc=" + nombrecon + "&emc=" + emailcon + "&ocuc=" + ocupacioncon;
    window.location = "'"+url+"'";     

Is it possible what I'm trying to do?

asked by Luis C. 26.12.2018 в 23:43

1 answer


If you want to avoid data through the URL I advise you to use HTML instead of Javascript:

<form method="GET" action="(tu url de destinacion">
    <input type="text" id="id">
    <input type="email" id="em" placeholder="E-Mail">
    <!-- mas datos que quieras pasar-->
    <input type="submit" value="enviar">

If you use the GET method it allows you to send your information encoded in the header of the HTTP request, that is, directly in the URL.


Is it possible what I'm trying to do?

The answer is yes, but you would complicate your life and it would not make sense since it would create a duplicate of the form html.

I hope it helps you.

answered by 27.12.2018 в 08:39