• Resolved netzlounge

    (@netzlounge)


    How can I export and insert form fields values of a contact form to an external database table (on same server)?

    • This topic was modified 6 years, 10 months ago by netzlounge.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Have you tried wpcf7_before_send_mail? I found cf7_external_db2018.php in Github (by mheywood90). This version is updated for WP 4.9.

    function wpcf7_send_to_external ( $cf7 ) {
    
    	//external db details
    	$username = 'username';
    	$password = 'password';
    	$database = 'database';
    	$host = 'host_ip';
    
    	//create new wpdb instance
    	$mydb = new wpdb($username, $password, $database, $host);
        
        	//limit hook to only fire on particular form ID (optional)
    	    if ( $cf7->id == 1 ) {
    
    		//get select box for different enquiry types (optional)
    		$type = $cf7->posted_data["your-select"];
    
    		//if form type is equal to above value (optional)
    		if ( $type == 'Form Name' ){
    
    			//code added for wordpress 4.9 2018
    			$cf7 = WPCF7_ContactForm::get_current();
        			$submission = WPCF7_Submission::get_instance();
    			$data = $submission->get_posted_data();
    			
    			
    			//get posted form fields
    			//these are example form fields
    			$field1 = $data["name"];
    			$field2 = $data["email"];
    			$field3 = $data["address"];
    			$field4 = $data["city"];
    			$field5 = $data["state"];
    			$field6 = $data["zip"];
    			$field7 = $data["phone"];
    			
    			
    			//insert into external db
    			$mydb->insert( 
    				//name of external db table
    				'table_name',
    				//name of table columns and which fields to insert 
    				//these are example fields
    				array( 
    					'name' => $field1, 
    					'email' => $field2,
    					'address' => $field3,
    					'city' => $field4,
    					'state' => $field5,
    					'zip' => $field6,
    					'phone' => $field7	
    
    				),
    				//field formats: %s = string, %d = integer, %f = float
    				array( 
    					'%s','%s','%s','%s','%s','%s','%s'
    				) 
    			);
    
    		}
    
    	}
    
    }
    add_action("wpcf7_before_send_mail", "wpcf7_send_to_external");
    Thread Starter netzlounge

    (@netzlounge)

    Indeed, I tried it a couple hours ago – I just had to comment out the if-clause “Form Name”, then it worked.

    Many thanks to you

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to insert values in external database table’ is closed to new replies.