Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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...

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 http://stackoverflow.com/a/6753281/1445460

share|improve this question
add comment

2 Answers

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")
share|improve this answer
    
As you're already using AJAX: Why don't you use the WordPress AJAX API? –  kaiser Nov 13 '13 at 18:41
add comment

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' );
share|improve this answer
    
Well its actually being called from a plugin, if that matters. I updated to show the section that prints the form. I would have to put my redirect after the $this->addto_db();. –  Kirill Fuchs Sep 26 '12 at 1:06
    
the code may be in a plugin, but your display_form function is being called from the template or an action that occurs in the template, processing needs to happen before output begins. –  Milo Sep 26 '12 at 1:52
    
How do I get it to happen before output begins? I tried add_action( 'init', array($this, 'addto_db')); but that didn't do anything. It didn't seem to call my function :(. Am I missing something? –  Kirill Fuchs Sep 26 '12 at 1:55
    
So this is an example of exactly what I'm trying to do. stackoverflow.com/a/6753281/1445460 My problem is I don't know how to redirect to paypal after I have done all the database stuff. –  Kirill Fuchs Sep 26 '12 at 1:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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