1

Intro:

I have a function that handles a contact form variables in my website.

  • user enters name, email and text.
  • function.php gets variables using javascript.
  • I get more information on the user on the way - like IP, Country, UTM tags if any, ect.

Then I added a code part that saves the variables respectively on a SQL table.

I created the table 'wp_contact_form' using phpMyAdmin.

used this code part in a function:

global $wpdb; 

$wpdb->insert( 
    'wp_contact_form', 
    array( 
        'con_ip'          => $_COOKIE['ip'], 
        'con_name'        => $fullname, 
        'con_email'       => $email, 
        'con_text'        => $message, 
        'con_country'     => $_COOKIE['country'], 
        'con_reigon'      => $_COOKIE['region'], 
        'con_city'        => $_COOKIE['city'], 
        'con_utm_source'  => $_COOKIE['utm_source'], 
        'con_utm_medium'  => $_COOKIE['utm_medium'], 
        'con_utm_campain' => $_COOKIE['utm_campaign'], 
        'con_utm_term'    => $_COOKIE['utm_term'], 
        'con_utm_content' => $_COOKIE['utm_content']
    ), 
    array( 
        '%s', 
        '%s', 
        '%s', 
        '%s', 
        '%s', 
        '%s', 
        '%s', 
        '%s', 
        '%s'
    ) 
);

And I still get a blank table. I tried following this: https://codex.wordpress.org/Class_Reference/wpdb#INSERT_rows

without any success.

db structure: https://i.stack.imgur.com/kQ8oZ.png enter image description here

Full function code:

/**
    Contact form using Ajax 
**/ 

add_action('wp_ajax_nopriv_submit_contact_form', 'submit_contact_form'); 

// Send information from the contact form 
function submit_contact_form(){

    // Get the UTM variables from the 'get_the_utm_vars()' function
    //$utm = get_the_utm_vars();

    // If there is a $_POST['email']...
    if( isset($_POST['email']) && ($_POST['validation'] == true ) ) {

        // Get parameters
        $email = $_POST['email']; // Gets the email of the user..
        $email_to = "arik@ example.pro";
        $utm_emails = array(
            'tova@ example.pro',
            'yonatan@ example.pro', 
            'arik@ example.pro',
            'gal@ example.pro',  
            'shai@ example.pro',  
            'walid@ example.pro'        
            );
        $fullname = $_POST['fullname'];
        $message = $_POST['text']; 
        $email_subject = " example Intro: $email";      
        $headers = array(
                'From: '. $fullname .' <'. $email .'>', 
                'BCC:  yonatan@ example.pro', 
                'BCC:  gal@ example.pro', 
                'BCC:  eran@ example.pro', 
                'BCC:  tova@ example.pro', 
                'BCC:  walid@ example.pro', 
                'Content-type: text/html; charset=\"UTF-8\"; format=flowed \r\n'
            ); 
        $utm_headers = array(
            'From: '. $fullname .' <'. $email .'>'
            );


        // Send email to YH, and if sent - do:
        if ( wp_mail($email_to,$email_subject,$message,$headers) ) {

            // Tells me that the mail has been sent
            echo json_encode( array("result"=>"complete") );

            //Add the UTM variables to the emails text
            $message .= "\r\n \r\n \r\n IP: ". $_COOKIE['ip'] ."\r\n Country: ". $_COOKIE['country'] ."\r\n Region: ". $_COOKIE['region'] ."\r\n City: ". $_COOKIE['city'] ." \r\n UTM Source: ".$_COOKIE['utm_source']." \r\n UTM Medium: ".$_COOKIE['utm_medium']." \r\n UTM Campaign: ".$_COOKIE['utm_campaign']."\r\n UTM Term: ".$_COOKIE['utm_term']." \r\n UTM Content: ".$_COOKIE['utm_content']." ";
            // A mail for tova with the UTM paramseters
            wp_mail($utm_emails,$email_subject,$message,$utm_headers);


        } else {
            echo json_encode(array("result"=>"mail_error"));
            var_dump($GLOBALS['phpmailer']->ErrorInfo);
    }
        wp_die();
    }










    global $wpdb; 

    $wpdb->insert( 
        'wp_contact_form', 
        array( 
            'con_ip'          => $_COOKIE['ip'], 
            'con_name'        => $fullname, 
            'con_email'       => $email, 
            'con_text'        => $message, 
            'con_country'     => $_COOKIE['country'], 
            'con_reigon'      => $_COOKIE['region'], 
            'con_city'        => $_COOKIE['city'], 
            'con_utm_source'  => $_COOKIE['utm_source'], 
            'con_utm_medium'  => $_COOKIE['utm_medium'], 
            'con_utm_campain' => $_COOKIE['utm_campaign'], 
            'con_utm_term'    => $_COOKIE['utm_term'], 
            'con_utm_content' => $_COOKIE['utm_content']
        ), 
        array( 
            '%s', 
            '%s', 
            '%s', 
            '%s', 
            '%s', 
            '%s', 
            '%s', 
            '%s', 
            '%s'
        ) 
    );







}

1 Answer 1

1

In your code, if an email is set and validated (which I suppose should be the normal case), your code will exit at wp_die(), and never reaches the $wpdb->insert.

Your $wpdb->insert statement will only ever be reached if an email is not set, or if "validation" is false.

You might want to get rid of the wp_die() call altogether, or place that call at the very end of your function, depending on your needs.

Other than that, try determining whether your function is called at all (might be something wrong with the AJAX call itself).

2
  • Well, emails DO get sent, so I'm not sure that there might be an Ajax problem (correct me if I'm wrong in this case..) I moved the part $wpdb->insert....... inside the "true" if statement, right after wp_mail($utm_emails,$email_subject,$message,$utm_headers); It's still not saving anything inside the DB table. What else can be wrong :'(
    – Kar19
    Aug 13, 2015 at 14:54
  • If the email does get sent, and that your $wpdb->insert... still does not add an entry to your table, there must be something wrong with the data you are trying to save: a null value (I noticed that all your table columns are defined with "NOT NULL"), for instance. Have you checked the mysql log? To check whether it is the data that is wrong, you might want to try to insert a "fixed" entry, ie, an entry where all the values are hardcoded. If that entry gets saved, then you will know it is the data at fault.
    – Philippe
    Aug 15, 2015 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.