Use admin-post to submit form data to external database

I am creating a custom form in WordPress to store and send data to an external database. As part of testing, I was successful in creating a form that submitted form data to the external database by having the form refer back to itself upon clicking the submit button.

I now would like to leverage WordPress admin-post functionality instead of having the form refer back to itself. I have set up the action hook correctly as I see the $_POST variables displayed once the form is submitted using:

form action="?php echo esc_url( admin_url( 'admin-post.php' ) ); ?" method="post" id="test" 

My question, can an external database be used with admin-post? At this point, when referencing the global variable for the external database connection, a PHP error is thrown stating that insert is referring to a NULL value. Below is relevant code pertaining to the issue...

?php
class x94 {

    public function __construct(){
        add_action( 'admin_post_submitForm9', array( $this, 'formHandling' ), 11, 1 );
        add_action( 'wp_head', array( $this, 'externalDB' ), 10, 1 );
    }

    //External DB connection
    function externalDB(){ 
        global $externalDB;
        $externalDB = new wpdb(DB_USER2, DB_PASSWORD2, DB_NAME3, DB_HOST2);
    }

    //create form
    function userForm() {
    //Global Variables

        global $externalDB;
        //print_r($externalDB);

        // starts output buffering
        ob_start(); 
        ?
        form action="?php echo esc_url( admin_url( 'admin-post.php' ) ); ?" method="post" id="test" 

            input type="hidden" name="action" id="userForm_action" value="submitForm9" /
            input type="text" name="visitor_name" id="visitor_name" / 
            input type="text" name="visitor_age" id="visitor_age" / 
            input type="text" name="visitor_gender" id="visitor_gender" / 

            input type="submit" name="submit_form" value="submit" /

        /form
        ?php
        $html = ob_get_clean();

        if ( isset( $_POST["submit_form"] )  $_POST["visitor_name"] != "" ) {
            //$_POST Variables
            $name = strip_tags($_POST["visitor_name"], "");
            $age = $_POST['visitor_age'];  
            $gender = $_POST['visitor_gender']; 



        }
        // if the form is submitted but the name is empty
        if ( isset( $_POST["submit_form"] )  $_POST["visitor_name"] == "" )
            $html .= "pYou need to fill the required fields./p";
        // outputs everything
        return $html;

    }

    function formHandling(){

        global $externalDB;
        print_r($externalDB); //not displaying data, global not recognized
        print_r($_POST); //displays data

        if (1 == 1){

            $externalDB-insert( 
                'basic_user_info', 
                array( 
                    'name' = $name, 
                    'age'  = $age,
                    'gender'= $gender,

                ),
                //field formats
                array('%s',
                      '%s',
                      '%s',
                      )
            );
            //$html = "pYour name strong$name/strong was successfully recorded. Thanks!!/p";
            die();
        }
    }
}//end class

//shortcodes
add_shortcode('basic-info', array( 'x94', 'userForm' ) );


new x94();
?

Topic remote plugin-development forms database Wordpress

Category Web


I'm answering this question to help future WordPress developers on their quest for knowledge. The answer is YES, you can connect to an external database when using the admin_post action. Below is the corrected source...

<?php
class x94 {

    private $externalDB01;

    public function __construct(){
        add_action( 'admin_post_submitForm9', array( $this, 'formHandling' ), 11, 1 );
        add_action( 'plugins_loaded', array( $this, 'externalDB' ), 10, 1 );
    }

    //External DB connection
    function externalDB(){ 
        $this->externalDB01 = new wpdb(DB_USER2, DB_PASSWORD2, DB_NAME3, DB_HOST2);
    }

    //create form
    function userForm() {

        ob_start(); 
        ?>
        <form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" id="test" >

            <input type="hidden" name="action" id="userForm_action" value="submitForm9" />
            <input type="text" name="visitor_name" id="visitor_name" /> 
            <input type="text" name="visitor_age" id="visitor_age" /> 
            <input type="text" name="visitor_gender" id="visitor_gender" /> 

            <input type="submit" name="submit_form" value="submit" />

        </form>
        <?php
        $html = ob_get_clean();
        return $html;

    }

    function formHandling(){

        if ( isset( $_POST) ) { 

            //sanatize data from the admin-post array
            $name   = sanitize_text_field( $_POST['vistor_name'] );
            $age    = sanitize_text_field( $_POST['vistor_age'] );
            $gender = sanitize_text_field( $_POST['vistor_gender'] );

            //submit data to external database
            $externalDB->insert( 
                'basic_user_info', 
                array( 
                    'name' => $name, 
                    'age'  => $age,
                    'gender'=> $gender,

                ),
                //field formats
                array('%s',
                      '%s',
                      '%s',
                      )
            );
        }
        else {

            wp_die();
        }
    }
}//end class

//shortcodes
add_shortcode('basic-info', array( 'x94', 'userForm' ) );


new x94();
?>

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.