Site icon Hip-Hop Website Design and Development

Use admin-post to submit kind information to exterior database

I’m making a customized kind in WordPress to retailer and ship information to an exterior database. As a part of testing, I used to be profitable in making a kind that submitted kind information to the exterior database by having the shape refer again to itself upon clicking the submit button.

I now wish to leverage WordPress admin-post performance as an alternative of getting the shape refer again to itself. I’ve arrange the motion hook accurately as I see the $_POST variables displayed as soon as the shape is submitted utilizing:

<kind motion="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" methodology="submit" id="check" >

My query, can an exterior database be used with admin-post? At this level, when referencing the worldwide variable for the exterior database connection, a PHP error is thrown stating that insert is referring to a NULL worth. Under is related code pertaining to the problem…

<?php
class x94 {

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

    //Exterior DB connection
    operate externalDB(){ 
        world $externalDB;
        $externalDB = new wpdb(DB_USER2, DB_PASSWORD2, DB_NAME3, DB_HOST2);
    }

    //create kind
    operate userForm() {
    //International Variables

        world $externalDB;
        //print_r($externalDB);

        // begins output buffering
        ob_start(); 
        ?>
        <kind motion="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" methodology="submit" id="check" >

            <enter kind="hidden" identify="motion" id="userForm_action" worth="submitForm9" />
            <enter kind="textual content" identify="visitor_name" id="visitor_name" /> 
            <enter kind="textual content" identify="visitor_age" id="visitor_age" /> 
            <enter kind="textual content" identify="visitor_gender" id="visitor_gender" /> 

            <enter kind="submit" identify="submit_form" worth="submit" />

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

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



        }
        // if the shape is submitted however the identify is empty
        if ( isset( $_POST["submit_form"] ) && $_POST["visitor_name"] == "" )
            $html .= "<p>That you must fill the required fields.</p>";
        // outputs all the pieces
        return $html;

    }

    operate formHandling(){

        world $externalDB;
        print_r($externalDB); //not displaying information, world not acknowledged
        print_r($_POST); //shows information

        if (1 == 1){

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

                ),
                //area codecs
                array('%s',
                      '%s',
                      '%s',
                      )
            );
            //$html = "<p>Your identify <sturdy>$identify</sturdy> was efficiently recorded. Thanks!!</p>";
            die();
        }
    }
}//finish class

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


new x94();
?>