title: Is it possible to exchange session data between ajax-calls and normal request? (in Wordpress)
body: <p>I'm using the <a href="https://wordpress.org/plugins/wp-session-manager/" rel="noreferrer">https://wordpress.org/plugins/wp-session-manager/</a> plugin and it works if I just set and get variables. (so the actual session managment works)</p>
  
  <p>The thing I want to is to exchange session-data between ajax-calls and "normal request to browser". Is this even possible? </p>
  
  <p><strong>functions.php</strong></p>
  
  <pre><code>add_action( 'wp_ajax_nopriv_set_formvalues', array ('FormHandler', 'set_formvalues') );
  add_action( 'wp_ajax_set_formvalues', array ('FormHandler', 'set_formvalues') );
  
  //Use of Plugin WP Session manager (so we can use sessions for storing current form values)                
  //(do this with ajax before submitting the form)
  global $wp_session;
  $wp_session = WP_Session::get_instance();        
  $formhandler = new FormHandler();
  
  class FormHandler {                
          public function get_current_formvalues() {
                  return $wp_session['formvalues'];
          }
  
          public function set_formvalues() {           
                  //Default values for form
                  $formvalues = array('name' =&gt; '', 
                                                              'address' =&gt; '', 
                                                              'plz' =&gt; '', 
                                                              'city' =&gt; '', 
                                                              'phone' =&gt; '',
                                                              'fax' =&gt; '',
                                                              'contactfirstname' =&gt; '',
                                                              'contactlastname' =&gt; '',
                                                              'contactanrede' =&gt; '',
                                                              'email' =&gt; '',
                                                              'contactphone' =&gt; '',
                                                              'recall' =&gt; '',
                                                              'newsletter' =&gt; '',
                                                              'messageText' =&gt; '');
  
                  //Change values in formvalues-array if values are posted
                  foreach($formvalues as $key =&gt; $value) {
                          if (isset($_POST[$key])) {
                                  $formvalues[$key] = $_POST[$key];
                          }
                  }
                  $wp_session['formvalues'] = $formvalues;       
                  echo json_encode( $wp_session );
                  die(); //Needed for wordpress ajax
          }
  
      public function __construct() {
          $formvalues = $this-&gt;get_current_formvalues();
          echo $formvalues //doesn't echo out nothing
      }
  
  }
  
  $n = new FormHandler(); //doesn't echo out anything
  </code></pre>
  
  <p>etc...</p>
  
  <p>From javascript I call the <code>set_formvalues()</code> through ajax and the values are returned (as json) correctly. </p>
  
  <p>I have to submit a form to another domain, but before I have to save the formvalues somehow because the domain I post redirects my pagebut only with a status code.  My thought was I could sent these formvalues through ajax, and then fetch them at next time my page is accessed so the visitor won't have to fill in all values if the user makes one single mistake.</p>
  
  <p><strong>Is it possible to exchange between ajax-calls and normal request and then how would I achieve that?</strong> unfornutenaly I must live with the "redirection-solution from the other server". Maybe it's possible but what am I doing wrong then? Any ideas?</p>
  
  <p><strong>UPDATE</strong>
  Even if I got it working with global variables, <strong>I wasn't satisfied with that</strong>. It really don't like globals, but I couldn't figure a way of doing this before, but now I've got a solution that I thought I wanted to share with you if someone stumbles upon similar issue(s):</p>
  
  <pre><code>class FormHandler {      
          private $wp_session = null; //Use of Plugin WP Session manger, so we can handle sessions
  
          public function get_current_formvalues() {
                  return $this-&gt;wp_session['formvalues'];
          }
  
          public function set_formvalues() {    
                  if ($this-&gt;wp_session === null) {
                      $this-&gt;wp_session = WP_Session::get_instance();                
                  }
  
                  //set $formvalues based on posted fields...
  
                  $this-&gt;wp_session['formvalues'] = $formvalues;
  
                  echo json_encode( $this-&gt;wp_session );
                  die(); //Needed for wordpress ajax
          }
  
          public function __construct() {                
                  //This makes it possible to use $this within function set_formvalue when using ajax in Wordpress    
                  add_action( 'wp_ajax_nopriv_set_formvalues', array ( $this, 'set_formvalues') );
                  add_action( 'wp_ajax_set_formvalues', array  ($this, 'set_formvalues') );                
                  $this-&gt;wp_session = WP_Session::get_instance();
          }
  
          public function display_contactform() {
                  $formvalues = $this-&gt;get_current_formvalues();
          }
  }
  </code></pre>