0

I have an ajax form that I'm working with. I need to detect if a user made a change to the status dropdown. I am attempting to set a variable thinking that the variable will still have the value I set when the form was submitted, but it does not.

How can I set a variable that will remain set so I can test against it?

if(typeof checkStatus == 'undefined'){
  alert('checkStatus is undefined');
  var checkStatus = status;
  // I'm expecting the next time the form is submitted, checkStatus will be set, but it's not
}else{
  if(checkStatus == status){
    alert('checkStatus is equal to ' + status);
  }
}

http://jsfiddle.net/7WU3M/

NaN
  • 1,286
  • 2
  • 16
  • 29

1 Answers1

0

If your form reloads the page (or goes somewhere else) on submit, then you would probably need to set a cookie. A cookie would keep the variable even when a page reloads or you browse away. Other javascript variables will be lost unless they are posted with the form and then retrieved on the next page.

You can find information on setting cookies here: How do I set/unset cookie with jQuery?

Community
  • 1
  • 1
RichardB
  • 2,615
  • 1
  • 11
  • 14
  • I think I may just use `window.myValue`. – NaN Jun 21 '14 at 21:50
  • @NaN: you realise that won't survive past the unloading of the page? – David Thomas Jun 21 '14 at 21:52
  • Actually, no, I didn't. There isn't a way to do this without using a cookie? The page doesn't actually refresh, it's AJAX – NaN Jun 21 '14 at 21:54
  • You could also use server-side sessions or get/post variables depending on how your form works. Just to check : is your checkstatus script on the form page, or on the ajax-loaded page? – RichardB Jun 21 '14 at 21:57
  • It's on the ajax loaded page. It's a function actually that is loaded on pageload – NaN Jun 21 '14 at 21:58
  • On your ajax page, how are you retrieving the post data? (May help if you post the full form and the script you are posting it to) – RichardB Jun 21 '14 at 22:13
  • @DavidThomas, I have a question. A window variable is what works for me. I know they are bad to use but since it's working, does this mean that I can use a regular `var` in its place? – NaN Jun 21 '14 at 23:00