Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWebsite Owner Tools

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Submit Form to Multiple URLs With PHP

A form submission to a PHP script can be relayed to another URL. The effect is a submission to two different scripts.

As an example of use, you capture the email address right away and then submit the form to a third-party newsletter mailing service. The form submits to your own script to do the capture. And your script relays the form submission to the mailing service.

As another example, you want to capture the buyer's name, email address, and ID of the product being purchased before submitting the purchase data for payment to PayPal (or to ClickBank, Authorize.net, Stripe, or other payment gateway).

Similar to the previous example, the form submits to your PHP script. Your script captures the information you want. It then relays the form submission to the payment gateway.

(To use Ajax for multiple form submission instead of a PHP relay, see Submit Form to Multiple URLs With Ajax.)

The source code of the PHP script accompanying this article sends an email to you with the content of the form that was submitted. Then it relays the form information to the next destination.

The PHP script may be updated by you to also store the form information to a file or MySQL table along with (or instead of) emailing the information to you. And/or to send you a text message. Or anything else a PHP script can do.

Two caveats:

  1. This system won't process file upload form fields.

  2. This system is unable to successfully relay to the URL of form scripts requiring CAPTCHA or other authentication.

There is no example in this article because there is no action to show. All action is done on the server after the form is submitted.

When your form submits to the PHP script accompanying this article, it:

  1. Accepts the form submission.

  2. Sends the form data to the email address specified in the PHP script.

  3. Submits the form data to the URL specified in the PHP script.

Here is the source code of the PHP script. Customization notes follow.

<?php
/* 
   Form Submission Relay
   Version 2.0
   
   Version 1.0, August 21, 2011
   Version 2.0, November 27, 2016, updated to:
      1. Include stripping slashes from form data before relaying.
      2. Publish submit button in case of slow internet or server.

   Will Bontrager Software, LLC
   https://www.willmaster.com/
   Copyright 2011,2016 Will Bontrager Software, LLC

   This software is provided "AS IS," without 
   any warranty of any kind, without even any 
   implied warranty such as merchantability 
   or fitness for a particular purpose.
   Will Bontrager Software LLC grants you 
   a royalty free license to use this software 
   provided this notice appears on all copies. 
*/

# Specify the URL where the form contents shall be submitted to.

$finalDestination = "http://example.com/formprocessing.php";


function TasksToDo()
{
# This example sends an email with the form contents.
# It may be deleted. If kept, update the email address below.
# Other tasks may be added.
   $emailAddress = "name@example.com";
   $emailSubject = "Form contents!";
   $dataToSend = '';
   foreach( $_POST as $k => $v )
   {
      $dataToSend .= "$k = $v\r\n\r\n";
   }
   mail($emailAddress, $emailSubject, $dataToSend, "From: $emailAddress");
} // end of function TasksToDo()


$Method = "post";
if( ! count($_POST) )
{
   $Method="get";
   $_POST = $_GET;
   $_GET = array();
}
$formFields = '';
foreach( $_POST as $kk => $vv )
{
   $k = stripslashes($kk);
   $v = stripslashes($vv);
   if( empty($_POST[$k]) ) { unset($_POST[$kk]); }
   $_POST[$k] = $v;
   $formFields .= '<input type="hidden" name="' . htmlspecialchars($k) . '" value="' . htmlspecialchars($v) . '">';
}
TasksToDo();
echo <<<RELAYPAGE
<html><body>
<form id="form-submission-relay-ID-value" method="$Method" action="$finalDestination">
$formFields
<input id="form-submission-relay-submit-button" style="display:none;" type="submit" value="Click to proceed ...">
</form>
<script type="text/javascript">
document.getElementById("form-submission-relay-ID-value").submit();
function DisplayButton() { document.getElementById("form-submission-relay-submit-button").style.display="block"; }
setTimeout(DisplayButton,3000); // Display submit button if takes longer than 3 seconds to submit.
</script>
</body></html>
RELAYPAGE;
exit;
?>

Save the source code as relay.php or other PHP file name that is appropriate for your installation. Then do the customization.

File name relay.php will be used in these instructions.

Customization Notes:

  1. At line 26, is a line colored red.

    Replace http://example.com/formprocessing.php with the URL of the form's final destination. This is the URL the form data will be relayed to from this relay.php script.

  2. At line 29 through line 42 is function TasksToDo()

    TasksToDo() is where whatever tasks you want done is accomplished before the form data is relayed to the final destination URL.

    Currently, it sends an email with the form data. You'll need to customize:

    1. The name@example.com email address on line 34 to the address where the form data shall be sent to.

    2. The Form contents! email subject line on line 34 to the subject line you prefer for the email being sent to the above address.

    The emailing may be deleted from TasksToDo() and other tasks may be added to that function. Any tasks PHP can do may be added to TasksToDo()

    You'll need to know how to program with PHP for adding tasks to TasksToDo(). Depending on what functionality you wish to implement, a search on the internet may provide example code.

To use the script, upload the customized relay.php to your server and make a note of its URL. Then change your form's action attribute URL to the URL of relay.php

The system is now ready to test. Submit the form and verify the things it is designed to do actually get done.

The relay.php script can process forms submitted with method POST and method GET, either way.

When a form is submitted, relay.php does its tasks and then sends a self-submitting form to the browser. The self-submitting form submits itself to the final destination.

(This article first appeared in Possibilities ezine.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC