0

If anyone can help with this, it would be greatly appreciated!

I have an HTML form, with a drop down for the state you’re in.

Then I have a JavaScript function that determines the state picked, and redirects the user accordingly.

  function pickstate()
{
        if(document.drop_list.State.value == "AR" ){
            document.drop_list.action = "www.theurl.com";
            document.drop_list.submit();
        }
}

My question is: can I use this method (and if so, how) to pass the state to the next form, so that when the user is redirected, the dropdown says the state they picked.

I know how to do this using PHP and POST, but this is not posting, it’s just a redirect to the correct URL based on the state, and since 20 or so states will use the same form, I don't want to make the user select their state again.

The reason I’m using this method is not everyone will be redirected — many states will stay on the form that is first displayed.. This is why I can’t use POST.

Help! Thank you.

3
  • 3
    If this is within a GET form, why not just grab the parameter from the query string on the second page? Commented Nov 15, 2012 at 22:37
  • 2
    (And those that aren't redirected can retrieve the variable from the same page.) Commented Nov 15, 2012 at 22:37
  • The form must post (in json) but this redirect is before the submit. Commented Nov 16, 2012 at 0:59

2 Answers 2

1

You can add a hash to the URL

function pickstate()
{
    var stateVal = document.drop_list.State.value;
    switch(stateVal) {
      case "AR"
        document.drop_list.action = "www.theurl.com/#"+stateVal;
      break;
      case "IT"
        document.drop_list.action = "www.theurl.com/#"+stateVal;
      break;
    }

    document.drop_list.submit();
}

Then the next form can select the state by using the passed hash

window.location.hash; // eg. will be equal to #AR or #IT
Sign up to request clarification or add additional context in comments.

Comments

0

You could store the value in local storage.

function pickState() {
    if (document.drop_list.State.value == "AR") {
        document.drop_list.action = "www.theurl.com";
        localStorage.setItem('selectedState', 'AR');
        document.drop_list.submit();
    }
}

Then add another functions that sets the value.

function setState() {
    var storedSelectedState = localStorage.getItem('selectedState');

    if (storedSelectedState) {
        document.drop_list.State.value = storedSelectedState;
    }
}

3 Comments

Slight overkill, isn't it, just to pass a value between forms?
Overkill? Probably. Expensive? Not at all.
I think I understand your method.. What would I add to the state field on the second page to get it to call the setState function? my code for the form currently is <SELECT id=State name=State>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.