如何在表单操作之前运行php

I have a form that runs a php file that stores data to a wp database however after it runs the php file I need to top function as a paypal button. The code is this at the moment:

<form id="candle-form" action="<?php bloginfo('template_directory');?>/db-submit.php" method="post">

  ....form body (works properly)

</form>

basically I need to run action="https://www.paypal.com/cgi-bin/webscr" after the php function so that the user can checkout with paypal. Is there any way to do this? I tried redirecting with Location: at the end of the php file but then the paypal link just goes to paypal.com and doesn't retain the form's info

For your form submission, submit to an external file.

You can run any operations before this code is set. Make sure there isn't anything modifying the headers. This code is verified to be working.

{
// Prepare GET data
$query = array();
$query['cmd'] = '_xclick';

$query['business'] = ''; //Your email or account ID
$query['lc'] = 'US'; //Location
$query['item_name'] = ''; //Item Name
$query['button_subtype'] = 'services'; //Button type
$query['no_shipping'] = '0'; //Shipping?
$query['bn'] = 'PP-BuyNowBF:btn_buynow_LG.gif:NonHosted'; //Button type
$query['amount'] = ''; //amount
$query['return'] = ''; //Return url after purchase
$query['cancel_url'] = ''; //If user cancels payment, url to take them to

// Prepare query string
$query_string = http_build_query($query);

header('Location: https://www.sandbox.paypal.com/us/cgi-bin/webscr?' . $query_string);
}