I want to save the data of a form in javascript variables and then fill another form with those variables

0

Script:

var texto = "";
function getData() {
    "use strict";
    texto = document.getElementById("texto").value;
}

HTML:

<html>
    <body>
        <script type="text/javascript" src="capturador.js"></script>
        <form id="form1" name="form1" method="get" action="form3.html" onsubmit="getData()">
            <label id="label1"></label><br>
            <input id="texto" type="text" name="texto" >
            <br>
            <input id="submit" type="submit" value="Enviar">
        </form> 
    </body>
</html>  

And this:

<html>
    <body>
        <script type="text/javascript" src="capturador.js"></script>
        <form id="form1" name="form1" method="get" action="" onsubmit="getData()">
          <label id="label1"></label><br>
          <input id="texto1" type="text" name="texto1" >
          <br>
          <input id="submit" type="submit" value="Mostrar Data">
        </form> 
    </body>
</html>

Sorry for the ignorance, I've only been watching videos for 2 days and working with javascript.

    
asked by psy 04.04.2018 в 22:20
source

2 answers

2

You could choose to use sessionStorage:

sessionStorage.setItem('key', 'value');

To recover the value:

var data = sessionStorage.getItem('key');

and to erase:

sessionStorage.removeItem('key') o sessionStorage.clear()
    
answered by 04.04.2018 / 22:34
source
0

Another way is using QueryString (as mentioned by @Kiko_L), where you get the input value from the URL, I'll give you an example:

HTML 1:

<html>
    <head>
        <title> </title>
        <meta charset="UTF-8">
    </head>
        <form action="get.html">
            <input type="text" name="serialNumber" />
            <input type="submit" value="Submit" />
        </form>
    <body>
    </body>
</html>

HTML 2:

                            

<body>
<div id="write">
</div>
<script>
    function getParamValuesByName (querystring) {
        var qstring = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for (var i = 0; i < qstring.length; i++) {
            var urlparam = qstring[i].split('=');
            if (urlparam[0] == querystring) {
            return urlparam[1];
            }
        }
    }
    var uid = getParamValuesByName('serialNumber');

    document.getElementById("write").innerHTML = uid;
</script>

In HTML 2 I assign the value of the input to a variable (uid) to the "write" div.

I hope this example helps.

    
answered by 05.04.2018 в 00:36