• Hello, have some problem getting data from form to send to a MySQL db.
    All this code is in the functions.php file.

    remove_all_filters ('wpcf7_before_send_mail');
    add_filter("wpcf7_before_send_mail","wpcf7_before_send_mail_function");
    add_action("wpcf7_before_send_mail","wpcf7_before_send_mail_function");

    I use this code to add the hook. (which is below the function code)

    function wpcf7_before_send_mail_function($contact_form) {
    	$title = $contact_form->title;
    	$submission = WPCF7_Submission::get_instance();
    	if($title != "ContactForm7Title") {
    		return;
    	}
    	$data = $submission->get_posted_data();
    	$navn  = $data['navn'];
    	if(isset($data['aCheckBox'])) {
    		$navn = "something";
    	}
    	$con = new mysqli("host", "user", "pw", "db"); //The DB host is different from the website host.
    	$con->set_charset("utf8");
    	$sql = "Some INSERT INTO query that has been tested and works";
    	$con->query($sql);
    	$con->close();
    }

    I have googled around allot and it seems like there’s many different ways to make this work. With this code, I do get confirmation: success, but it doesnt add anything into the database. I have also tried without the if($title != match) but still does not work.

    I am doing this for someone else, and previously I got this to work, but after some WP updates, and plugin updates, it didn’t anymore. I have tried now for a long time.

    I am pretty sure the functions runs once I press submit. Because if I enter some faulty code, I won’t get any confirmation message by the form, nor will the email be sent.

    I suspect that I try to catch the form data wrong, or the mysqli stuff is wrong.

    https://wordpress.org/plugins/contact-form-7/

  • The topic ‘Hook and formdata to far away MySQL db’ is closed to new replies.