How to keep values in inputs with JQuery

0

Good afternoon,

In a php page, I have several text input fields. In this page I fill in each input with a value, 2.00, 3.00 ... How do I keep these values in the fields, in the inputs, even if I go to another page? For on the other page I will "pull" those values to make calculations with them. Each input has an id, of course, and I'm declared as global "window.varivel". I'm not saving the values of the inputs in a database, as they simply must, can be changed at any time, as an excel cell.

    
asked by anonymous 02.06.2015 / 22:37

1 answer

1

One suggestion is to use a query string . That is, parameters passed in the URL as a GET.

In a first phase together the IDs and their values in pairs id=valor and concatenated with & :

document.querySelector('button').addEventListener('click', function () {
    var inputs = document.querySelectorAll('input');
    var queryString = [];
    for (var i = 0; i < inputs.length; i++) {
        queryString.push(inputs[i].id + '=' + inputs[i].value);
    }
    window.location = 'http://sergiofrilans.se/test/teste_SOpt.html?' + queryString.join('&');
})

and in a second phase read this string to be able to retrieve the values.

var qs = location.search.slice(1).split('&');
for (var i = 0; i < qs.length; i++) {
    var keyValue = qs[i].split('=');
    document.getElementById(keyValue[0]).value = keyValue[1];
}

jsFiddle and other sandboxes prevent examples with window.location so I made an example online on my site that you can test here . If anyone knows of another fiddle / bin say so you can put it there.

    
02.06.2015 / 23:13