Submitting post to database then redirecting to paypal

I am trying to submit form data to the database then send the client over to paypal which will use IPN to send a response back.

Right now I have the form submitting to the database, I have the IPN working, my problem is the redirecting part. I want it all to be done in with the click of one button…

Read More

My form just submits back to the current page where it validates data and adds to the database. I can’t seem to use header because data is already sent to the header.

What are my options/solutions. Simplest solution would be best but I’m open to anything at this point.

For such a simple task you would think there would be a simple solution…

Here’s my code that prints out the form:

function display_form() { ?>
        <div class="culis-module">
            <form action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>" method="post">      
                <div class="clearfix culis-module-body">
                    <?php 
                    if ( $_POST['culis_add_listing'] == 'Y' && $this->no_errors()) { 
                        $this->addto_db();
                        ?><div class="alert alert-success"><button type="button" class="close" data-dismiss="alert">x</button> <?php _e('Thank You, your company will be listed as soon as payment is processed.')?></div><?php 
                    }

                    if ( $_POST['culis_add_listing'] == 'Y' && !$this->no_errors()) { 
                        $this->prepare_fields('saved'); 
                    } 
                    else {
                        $this->prepare_fields();
                    }?>
                </div>
                <div class="modal-footer">  
                    <button class="btn btn-primary" type="submit" name="culis_add_listing" value="Y">Submit</button>
                    <button class="btn" type="submit" name="form_reset" value="Y">Clear</button>
                </div>
            </form>
        </div>
    <?php 
    }

Update:
Here is an example of what I’m trying to do https://stackoverflow.com/a/6753281/1445460

Related posts

Leave a Reply

3 comments

  1. Not sure how I ran into this question one year later, but since it is unanswered…

    Instead of self-submitting the form and then redirecting to Paypal, you can instead have the paypal url as your form’s action, but use Ajax to save the form data before the form is submitted to Paypal. You can also use the Ajax response to prepare the form before submission, e.g. filling some hidden fields

    Here’s an example:

    myform.php:

    <form id="myform" method="post" action="<?php echo $paypal_url; ?>">
       <input type="text" ...>
       <input type="hidden" name="my-hidden-field" id="my-hidden-field" value="this is hidden"> 
       ...other form fields...
    </form>
    

    saveform.php: Processes the Ajax request and returns a response that can be used to manipulate the form before submission

    <?php 
      if (!empty($_POST)) {
         //save $_POST data to the database
         ...
         //also, fill some data as response
         $response = array('my_hidden_field' => 'this is now filled in');
         //next line returns the response
         echo json_encode($response);  
      }
    

    myform.js: Submits form data to saveform.php before submitting to paypal

    $j = jQuery.noConflict();
    
    $j(document).ready(function() {
        $j('#myform').submit(submit_myform);
    });
    
    function submit_myform() {
        if (!myform_is_valid()) {
            window.location.href = "#myform"; 
            return false;//prevent normal browser submission, to display validation errors
        } else {
            $j.ajax({
               url: 'saveform.php',
               type: 'POST',
               data: $j(this).serialize(),
               dataType:'json', //data type of Ajax response expected from server
               success: myform_success //callback to handle Ajax response and submit to Paypal
            });
           return false;//prevent normal browser submission, since the form is submitted in the callback
       } 
    }
    
    function myform_is_valid() {
        $valid = true;
        //validate data; if there's a validation error, set $valid=false and display errors on page
        ...
        return $valid;
    }
    
    function myform_success(response) {
        //this is called whenever the ajax request returns a "200 Ok" http header
        //manipulate the form as you wish
        $j('#my-hidden-field').val(response.my_hidden_field);
        //submit the form (to the form's action attribute, i.e $paypal_url)
        document.forms['myform'].submit(); 
    }
    

    Instead of the jquery.ajax call you can also use the shorter:

    $j.post('saveform.php',$j('#myform').serialize(),myform_success,"json")
    
  2. Indeed there is a simple solution. I assume you’re doing the form processing and attempting to redirect in your template? You need to hook an earlier action, before headers are sent. example:

    function wpa66284_process_my_form(){
        if( isset( $_POST['myform'] ) ){
            // process form and redirect
        }
    }
    add_action( 'init', 'wpa66284_process_my_form' );
    
  3. $j = jQuery.noConflict();
    
    $j(document).ready(function() {
        $j('#myform').submit(submit_myform);
    });
    
    function submit_myform() {
        if (!myform_is_valid()) {
            window.location.href = "#myform"; 
            return false;//prevent normal browser submission, to display validation errors
        } else {
            $j.ajax({
               url: 'saveform.php',
               type: 'POST',
               data: $j(this).serialize(),
               dataType:'json', //data type of Ajax response expected from server
               success: myform_success //callback to handle Ajax response and submit to Paypal
            });
           return false;//prevent normal browser submission, since the form is submitted in the callback
       } 
    }
    
    function myform_is_valid() {
        $valid = true;
        //validate data; if there's a validation error, set $valid=false and display errors on page
        ...
        return $valid;
    }
    
    function myform_success(response) {
        //this is called whenever the ajax request returns a "200 Ok" http header
        //manipulate the form as you wish
        $j('#my-hidden-field').val(response.my_hidden_field);
        //submit the form (to the form's action attribute, i.e $paypal_url)
        document.forms['myform'].submit();
    

    enter image description here