Pass the ID number from one input to another on a different page

0

I need to do that when I enter a dni in a input and I give the boton to accept, that number, go to another input that is in another page. I have tried using localstorage but I have not been successful, I have also entered this in the html to save the data but it does not work either.

function tomardni() {
   var dniv = localStorage.setItem("DNI");
}
    
asked by Lucas Pupilli 10.09.2018 в 18:13
source

1 answer

2

You can do it in several ways: cookies, localstorage, as a parameter by URL with js and with the backend language, I do not even tell you anymore.

Example with localstorage:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <input type="text" id="txtDNI">
    <input type="button" value="Enviar" onclick="Enviar()">
</body>
<script>    
    function Enviar() {
        var dni = document.getElementById("txtDNI").value;
        console.log(dni);
        localStorage.setItem("DNI", dni);
        window.location.href = "ejem2.html";
    }
</script>
</html>

Up to here you can save the DNI, if we perform the debug we will see how it is stored in localstorage

When the other page is loaded you will only need to invoke element of the localstorage with GetItem and the Id that was provided DNI

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body onload="loadDNI()" >
    <input type="text" id="txtGetDNI">
</body>
<script>
    var dni = localStorage.getItem("DNI");

    function loadDNI () { document.getElementById("txtGetDNI").value = dni; }
</script>
</html>

And you'll already get the item saved in localstorage

    
answered by 10.09.2018 / 19:31
source