How do you save form data into database before submitting

I am trying to save my form data into a wordpress database before submitting it. Please help. Heres my code and the php code to insert into database:

<form method="post" action="https://app.icontact.com/icp/signup.php" name="icpsignup" 

id="icpsignup1030" accept-charset="UTF-8" onsubmit="return verifyRequired1030();">
<input type="hidden" name="redirect" value="http://www.123.com/thank-you.html">
<input type="hidden" name="errorredirect" value="http://www.123.com/error.html">
<input type="hidden" name="listid" value="16360">
<input type="hidden" name="specialid:16360" value="MA3A">

<input type="hidden" name="clientid" value="1259610">
<input type="hidden" name="formid" value="1030">
<input type="hidden" name="reallistid" value="1">
<input type="hidden" name="doubleopt" value="0">
<input type="text" name="fields_email" size="21" style="padding:5px;">
<input type="image" name="submit" src="signup button top.png" value="" height="32px">

</form>

And the code to insert into DB:

<?php   
    require_once('../../../wp-config.php');
    global $wpdb;
    $table_name = "icontact_emails";
    $email = $_POST[fields_email];
    if (trim($email) != ""){
    $wpdb->insert($table_name, array('email' => $email, 'timestamp'=> date("Y- m-d h:i:s")));
    }
?> 

Related posts

Leave a Reply

4 comments

  1. “Submitting” is the only way to transfer it to the server, whether you submit the form via AJAX or traditional methods – no difference.

    Remember, this is a client-server model. The form is on the client, the DB is on the server.

  2. I can INFER from your question that you really mean: I am trying to save this form to a database before I submit it to ANOTHER SITE which I don’t control. Meaning you want to save it to your own database before you submit the data to a 3rd party.

    If that’s the case here’s what I recommend:

    Use the form’s onSubmit event (<form onSubmit="YourJavasScriptMethod();">). Use JQuery and $.ajax() to read all of your form inputs and submit them to your own script which can save the values to the database. At the end of your onSubmit method be sure to return true so the browser will continue to submit the form.

    Here’s a pseudo-code example:

    <form id="myForm" onSubmit="SaveToMyOwnDatabase();">
    
        <input />... //your inputs
    
    </form>
    

    and your javascript:

    function SaveToMyOwnDatabase()
    {
        // Create Json object by selecting each of the form inputs
        // Call $.ajax and submit the Json to your script which processes the json
    
        return true;
    }
    
  3. I would recommend using Contact Form 7 (WP plugin). There is a PHP hook built in that you can use to do this called wpcf7_before_send_mail. It will pass an object with all of the contact form data to your custom function(s) which you can use to write to the database or do other server side stuff. Here’s a (really) short example (this goes in functions.php):

    add_action( 'wpcf7_before_send_mail', 'save_contact_to_db' );
    function save_contact_to_db( $cf7 ) {
        $email = $cf7->posted_data['fields_email'];
        // put database writing stuff here
    }
    

    There is a little more info to be had here. Also this link should be helpful since it’s people discussing how to do exactly the same thing you are asking. There is also a plugin that will automatically write CF7 or Jetpack form submissions to the database for you but I’ve never used it so I can’t comment on it. In general though I recommend using contact form 7 for this, especially since you are sending the form post data to a php script that is not on your server.

  4. I think the solution is simple : submit your form to a PHP page that will : 1) save it in your database and 2) send it wherever you want. By far the simplest and most used solution in your case!