Maintaining input values with Jquery

Asked

Viewed 167 times

0

Good afternoon,

On a php page, I have several text input fields. On this page I fill each input with a value, 2,00, 3,00... How do I get these values to stay in the fields, in the inputs, even if I go to another page? Because on the other page "pull" these values to make calculations with them. Each input has an id, of course, and I am declaring them as global "window.varivel". I am not saving the values of the inputs in the database, because they simply must, can be changed at any time, as an excel cell.

  • I’m on a tight schedule but you can do this via query string that you move from one page to another: http://answall.com/a/429/129

  • Sorry @Sergio, but I don’t understand.

1 answer

1

A 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 you read this string so you can recover 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 "sandbox" prevent examples with window.location so I made an online example on my site that can be tested here. If anyone knows about another fiddle/bin tell them to put it there.

  • That’s the best way?

  • @Gustavosevero depends on the amount of data and the level of security you need. This is the simplest. More complex and not showing the information go by using the service or site Storage/indexDB.

  • What goes instead of "button" and "click" on your code like that? Because I don’t want to click on anything to do this, or rather, I wouldn’t like to have to click on anything, I’m just going to click on the link to go to the other page where I’m going to use the data.

  • @Gustavosevero changes document.querySelector('button') to select your link. If it’s more than one you have to cycle for. If you want a clearer example you have to put your HTML.

  • No, it’s a single link, if you mean me having more than one.

  • And forgive my ignorance, but how to select my link instead of "Document.querySelector('button')"?

  • @Gustavosevero not knowing how your HTML is can not help much. Get a selector that works for this link. If you have ID or class even better. For example: document.querySelector('.classe_do_pai_do_link a'). And you have to wear .preventDefault() for redirect to be with the query string.

  • Right. But my link has no class or id, it is a link <a href="link">link</a>

  • @Gustavosevero ^

Show 5 more comments

Browser other questions tagged

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