Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to exchange session data between ajax-calls and normal request? (in Wordpress)

I'm using the https://wordpress.org/plugins/wp-session-manager/ plugin and it works if I just set and get variables. (so the actual session managment works)

The thing I want to is to exchange session-data between ajax-calls and "normal request to browser". Is this even possible?

functions.php

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' => '', 
                                                            'address' => '', 
                                                            'plz' => '', 
                                                            'city' => '', 
                                                            'phone' => '',
                                                            'fax' => '',
                                                            'contactfirstname' => '',
                                                            'contactlastname' => '',
                                                            'contactanrede' => '',
                                                            'email' => '',
                                                            'contactphone' => '',
                                                            'recall' => '',
                                                            'newsletter' => '',
                                                            'messageText' => '');

                //Change values in formvalues-array if values are posted
                foreach($formvalues as $key => $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->get_current_formvalues();
        echo $formvalues //doesn't echo out nothing
    }

}

$n = new FormHandler(); //doesn't echo out anything

etc...

From javascript I call the set_formvalues() through ajax and the values are returned (as json) correctly.

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.

Is it possible to exchange between ajax-calls and normal request and then how would I achieve that? 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?

UPDATE Even if I got it working with global variables, I wasn't satisfied with that. 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):

class FormHandler {      
        private $wp_session = null; //Use of Plugin WP Session manger, so we can handle sessions

        public function get_current_formvalues() {
                return $this->wp_session['formvalues'];
        }

        public function set_formvalues() {    
                if ($this->wp_session === null) {
                    $this->wp_session = WP_Session::get_instance();                
                }

                //set $formvalues based on posted fields...

                $this->wp_session['formvalues'] = $formvalues;

                echo json_encode( $this->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->wp_session = WP_Session::get_instance();
        }

        public function display_contactform() {
                $formvalues = $this->get_current_formvalues();
        }
}
like image 483
bestprogrammerintheworld Avatar asked Sep 27 '22 01:09

bestprogrammerintheworld


1 Answers

Global keyword should be inside the function you want to use it in, like so:

public function get_current_formvalues() {
        global $wp_session;
        return $wp_session['formvalues'];
}

And also add it in the set method

 public function set_formvalues() {
         global $wp_session;           
         //Default values for form

The way it is currently written, $wp_session is not availble when called. You can read more on http://php.net/manual/en/language.variables.scope.php

like image 192
Skarlinski Avatar answered Nov 15 '22 04:11

Skarlinski