Support » Fixing WordPress » How to save form data to database after submit

  • Hello!
    Help please with save form data to database after submit.

    1. I created the shortcode with PayPal Form:

    function checkout_shortcode ($atts){
        ob_start(); 
    
        global $wpdb;
    
        $current_user = wp_get_current_user();
    
        echo "<div class='payform'>
        <form id='paymentForm' name='paymentForm' action='https://www.sandbox.paypal.com/cgi-bin/webscr' METHOD='POST'>
    	<input type='hidden' name='action' value='save_sdb_checkout_shortcode' />
            <input type='hidden' name='cmd' value='_xclick' />
    	<input type='hidden' name='business' value='mymail@com.com' />
            <input type='hidden' id='item_number' name='item_number' value='1'>
    	<input type='hidden' name='no_shipping' value='1'>
    	<input type='hidden' name='return' value=http://site.com/return/ />
    	<input type='hidden' name='cancel_return' value=http://site.com/cancel/ />
    	<input type='hidden' name='notify_url' value=http://site.com/notify/ />
    
        <label>Name:</label> <input  type='text' name='name' value='".$current_user->display_name."'>
        <label>Email:</label> <input  type='text' name='email' value='".$current_user->user_email."'>
        <label>Phone:</label> <input type='text' name='phone' value='".get_user_meta($user_id, 'phone', true)."'>
        <label>Pyment Purpose:</label> <input type='text' name='item_name'>
        <label>Payment Amount:</label> <input type='number' name='amount' min='0' max='100000' step='0.01'>
        <br><input type='submit' name='submit' value='Make a Payment' />
        </form></div>";
    
      $output = ob_get_clean();
      return $output;
    }

    2. After submit I need to save data to custom table:

    global $wpdb;
        $payment_data = array();
            $payment_data['user_id'] = get_current_user_id();
            $payment_data['amount'] = $_POST['amount'];
            $payment_data['status'] = 'Pending';
            $payment_data['payment_date'] = date('Y-m-d H:i:s');
            $payment_data['payment_type'] = 'Paypal';
            $payment_data['item_name'] = $_POST['item_name'];
            $wpdb->insert( $wpdb->get_blog_prefix() .
                           'payments', $payment_data );

    But I cant understand how to do it.
    I already tried many options(2 submits through js, Ajax) but no results (

Viewing 6 replies - 1 through 6 (of 6 total)
  • Using $wpdb->insert for multiple columns, you need to apply an array, example:

    $table = $wpdb->prefix."my_table_name";
    $wpdb->insert(
    	$table,
    	array(
    		'day' => 'today',
    		'post' => '12',
    		'stats' => '2'
    	)
    );

    Set the form, and if(isset($_POST["submit"])) do the inserting..

    Thank you, Samuel. I did it, but it’s doesn’t work.
    Because form has external action – action=’https://www.sandbox.paypal.com/cgi-bin/webscr&#8217;

    When I put “if(isset($_POST[“submit”]))” – it’s not running.
    Maybe I put it in wrong place?

    function checkout_shortcode ($atts){
        ob_start(); 
    
        global $wpdb;
    
        $current_user = wp_get_current_user();
    
        echo "<div class='payform'>
        <form id='paymentForm' name='paymentForm' action='https://www.sandbox.paypal.com/cgi-bin/webscr' METHOD='POST'>
    	<input type='hidden' name='action' value='save_sdb_checkout_shortcode' />
            <input type='hidden' name='cmd' value='_xclick' />
    	<input type='hidden' name='business' value='mymail@com.com' />
            <input type='hidden' id='item_number' name='item_number' value='1'>
    	<input type='hidden' name='no_shipping' value='1'>
    	<input type='hidden' name='return' value=http://site.com/return/ />
    	<input type='hidden' name='cancel_return' value=http://site.com/cancel/ />
    	<input type='hidden' name='notify_url' value=http://site.com/notify/ />
    
        <label>Name:</label> <input  type='text' name='name' value='".$current_user->display_name."'>
        <label>Email:</label> <input  type='text' name='email' value='".$current_user->user_email."'>
        <label>Phone:</label> <input type='text' name='phone' value='".get_user_meta($user_id, 'phone', true)."'>
        <label>Pyment Purpose:</label> <input type='text' name='item_name'>
        <label>Payment Amount:</label> <input type='number' name='amount' min='0' max='100000' step='0.01'>
        <input type='submit' name='submit' value='Make a Payment' />
        </form></div>";
    
    if(isset($_POST["submit"])){
        global $wpdb;
        $payment_data = array();
            $payment_data['user_id'] = get_current_user_id();
            $payment_data['amount'] = $_POST['amount'];
            $payment_data['status'] = 'Pending';
            $payment_data['payment_date'] = date('Y-m-d H:i:s');
            $payment_data['payment_type'] = 'Paypal';
            $payment_data['item_name'] = $_POST['item_name'];
            $wpdb->insert( $wpdb->get_blog_prefix() .
                           'payments', $payment_data );
    }
      $output = ob_get_clean();
      return $output;
    }

    It doesn’t matter the action of the form, it matters what you ask the server to do through PHP.

    I made an example, please look deeply into it, as it may help you:

    http://pastebin.com/Pws0zNCL

    Usage: put this [insert-into-db] into a new post/page, and make couple test inserts as it may help you with your project..

    Thank you, Samuel.
    Your example works if action empty (action=””). It’s mean that code will be interpreted by the same page. If you change action in your code for any external, for example action=”http://site.com/payment.php&#8221; – you will not save data to database.

    Hi.

    I am sorry about that, I see once the action is not the request URI or not empty, the form will send to the URL in the action..

    You can use some jQuery and AJAX to send data to your database first, then proceed with the payment action..

    So far, this is what I could have:

    $("#v_form").submit(function(){
    	alert($('#v_name').val());
    });

    You could prevent the default form behavior until you submit your data to db, then proceed with the action..

    How to add image insert filed with it?

    example:<label>User Photo</label> and display it ?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to save form data to database after submit’ is closed to new replies.