1

I have a JavaScript function

function reload2(form)
{   
    var val=form.loc.options[form.loc.options.selectedIndex].value;
    var val2 = form.category.options[form.category.options.selectedIndex].value;
    self.location='upload.php?category=' + val2;

}

In the function the values of val 1 is storing the value of id 'loc' and val2 is storing the value of id = 'category'.

The self.location is setting only category to value 2 but i also want the value of loc to become val1. ( so that on reloading, values of both loc and category are preserved).

What should i do in order to achieve that?

torox
  • 460
  • 1
  • 3
  • 11

2 Answers2

1

You look like you want the parameter from the url.

So change around your reload function:

function reload2(form)
        {   
            var val1=form.loc.options[form.loc.options.selectedIndex].value;
            var val2 = form.category.options[form.category.options.selectedIndex].value;
            self.location='upload.php?category=' + val2 + '&loc=' + val1;

        }

Then when the page is reloaded grab the new parameter:

//some on load functionality
var loc= getQueryParams(document.location.search).loc;

Using this function from How to get the value from the GET parameters?

function getQueryParams(qs) {
    qs = qs.split("+").join(" ");

    var params = {}, tokens,
        re = /[?&]?([^=]+)=([^&]*)/g;

    while (tokens = re.exec(qs)) {
        params[decodeURIComponent(tokens[1])]
            = decodeURIComponent(tokens[2]);
    }

    return params;
}
Community
  • 1
  • 1
Zach Leighton
  • 1,939
  • 14
  • 24
  • actually sir, the problem is that I am having four dropdownlist 'STATES' 'DISTRICT' 'CATEGORY' 'SUBJECT'. The value of 'DISTRICT' and 'SUBJECT' changes with respect to the values in 'STATE' and 'CATEGORY' respectively. I am using two different onchange functions for both 'STATE' and 'CATEGORY'. like this codepen.io/anon/pen/gpgLxB When I select 'STATE' the page refreshes and the value is preserved but after that when I select 'CATEGORY' the value of 'STATE' is getting back to default value i.e choose. How to preserve both these values in a single function? – user3315810 May 29 '15 at 10:21
0

you need to build up the query string like below.

function reload2(form)
            {   
                var val=form.loc.options[form.loc.options.selectedIndex].value;
                var val2 = form.category.options[form.category.options.selectedIndex].value;
                self.location='upload.php?category=' + val2 + '&loc=' + val;

            }
Josh Stevens
  • 3,943
  • 1
  • 15
  • 22
  • How does he access it when it reloads? – Zach Leighton May 28 '15 at 18:34
  • it didnt seem like he asked that.. he wanted to know how to get loc in the query string.. (which i answered). he has no code at all, no attempt to read it so i just supplied what he asked for.. if you want to know how to do it i can tell you but not answering something which he has not even given it ago at. – Josh Stevens May 28 '15 at 18:35
  • @JoshStevens actually sir, the problem is that I am having four dropdownlist STATES DISTRICT Category Subject. The value of district and subject changes with respect to the values in state and category respectively. I am using two different onchange functions for both state and category. like this http://codepen.io/anon/pen/gpgLxB When I select state the page refreshes and the value is preserved but after that when I select category the value of state is getting back to default value i.e choose. How to preserve both these values in a single function? – user3315810 May 29 '15 at 10:16