0

In Form_1 I'm setting the session :

   <form action="form_2.php" >
   <input type="text" id="zipcode" maxlength="7" name="zipcode">
   </form>


<script>
   $("form").submit(function(){
      var zipcode = $('#zipcode').val();   
      $.session.set("zipcode","zipcode");
      alert($.session.get("zipcode"));// NOT WORKING :(   
   });

</script>

Meanwhile, the session is not used nor destroyed.

In form_3 I'm getting the session but its not working :

 var zipcode=$.session.get('zipcode'); //NOT WORKING
2
  • open in both forms the browser developer tools with F12 and let us know what is in the sessionStorage via Tab Application -> Session Storage Commented Nov 20, 2017 at 12:17
  • i see no key and no value Commented Nov 20, 2017 at 12:19

2 Answers 2

1

Try following:

    // save data to sessionStorage
    sessionStorage.setItem('key', 'value');

    // get data from sessionStorage
    var data = sessionStorage.getItem('key');

Referring to following link: Window.sessionStorage

This example works:

<form action="form_2.php" >
   <input type="text" id="zipcode" maxlength="7" name="zipcode">
</form>


<script src="https://code.jquery.com/jquery-3.2.1.min.js integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="crossorigin="anonymous"></script>
<script>
   $("form").submit(function(){
      var zipcode = $('#zipcode').val();   
      sessionStorage.setItem("zipcode", zipcode);
      alert(sessionStorage.getItem("zipcode"));
   });    
</script>
Sign up to request clarification or add additional context in comments.

4 Comments

i used var data = sessionStorage.getItem('key'); alert (data); its alert null
but the session is filled and visible in developer tools?
No I don’t see In application session storage
Thanks for you @trura you made my day!
1

I suspect that the handler function needs to prevent the default form submission.

Assuming there is one form on the page:

$form = $("form")
$form.submit(function(e){
   e.preventDefault();
   var zipcode = $('#zipcode').val();   
   $.session.set("zipcode", zipcode);
   $form.submit();
});

2 Comments

@DUMB_CODER did you debug? put debugger; statement to somewhere in JS your code, open devtools, refresh the page. With limited visibility to your code, it is hard to find out what you are asking for :)
Thanks for your time @cmertayak the above one is working needed to add some link :) all i needed was pass value into session and use it

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.