Software, your way.
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!

Form Submission Relay
(Submit To Two Places)

This article shows how to relay a form submission. The effect is like submitting the form to two different URLs.

For example, a subscription form submits to a third-party newsletter mailing service. You also want it to submit to your own script so you can capture the email address right away.

To work it, the form submits to your own script first. And your script relays the form submission to the third-party newsletter mailing service.

For another example, you want to capture the buyer's name and email address from a form before submitting to PayPal (or to ClickBank or Authorize.net or other payment gateway) for the buyer to make payment arrangements.

To work it, the form submits to your script, where you capture the information, then your script submits the information to PayPal (or ClickBank, et cetera).

The PHP script accompanying this article will send an email to you with the contents of the form that was submitted to it. After sending the email, the PHP script will submit the form information to another URL.

You may replace the emailing section of the PHP script with other things for the script to do, if you wish. Make it save the data in a file or MySQL database, for example. Or send you a text message. Pretty much anything a PHP script can do.

Note #1: This system will not work with forms requiring CAPTCHA or other authentication. That's because the PHP script can not respond to CAPTCHA or other authentication requests in conjunction with relaying the form information to the next URL.

Note #2: This system will not work with forms containing file upload fields. However, it can be upgraded to work with file uploads. I left that part out because it would make the script appear complex. If you need to relay a form submission with a file upload field, let me know and I'll program it for you. I'll only charge you our minimum programming fee.

Here is the code for an example form. Note that it will be submitted to a script named relay.php (which is the script you'll find later in this article).

<form method="post" action="http://example.com/relay.php">
Email address: 
<input type="text" size="27" name="email">
<br>
Name:
<input type="text" size="27" name="name">
<br>
<input type="submit" value="Submit form">
</form>

The only change that needs to be made to your forms is the value of the form tag's action attribute. Its value needs to be changed to the URL of the relay.php script.

Here is the source code of relay.php:

<?php
/* 
   Form Submission Relay
   Version 1.0
   August 21, 2011

   Will Bontrager Software, LLC
   https://www.willmaster.com/
   Copyright 2011 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 or 
   modify 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 replaced with whatever tasks you want done.
$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();
}
TasksToDo();
$formFields = '';
foreach( $_POST as $k => $v )
{
   $formFields .= '<input type="hidden" name="' . htmlspecialchars($k) . '" value="' . htmlspecialchars($v) . '">';
}
echo '<html><body>
<form id="anIDvalue" method="' . $Method . '" action="' . $finalDestination . '">
' . $formFields . '
<input type="submit" value="Click to proceed ...">
</form>
<script type="text/javascript">
document.getElementById("anIDvalue").submit();
</script>
</body></html>';
exit;
?>

To install:

  1. Save the PHP script as relay.php (or other .php file name that you prefer).

  2. In the source code, below relay.php's header lines, at line 23, specify the URL that relay.php shall submit the form information to. It generally would be the URL of the form's old action value, the value before implementing this relay. But specify the full http://... URL.

    The reason for the full http://... URL is that a relative URL may be broken during the relay, which can happen if relay.php is not in the same directory as the form. So just use the full http://... URL and there should be no destination issues.

  3. In the function TasksToDo(), at lines 25 - 37, is where relay.php sends the email.

    You'll see two places to customize, the email address to send the form information to and the subject line for the email.

    If an email with the submitted form's information is all you want done before relay.php submits to the next URL, then go now to step 4.

    Otherwise, function TasksToDo() is where the edits go if you want to replace the email sending task with something else. Replace the code within the TasksToDo() function with the code to do the things that need to be done.

    Adding tasks to relay.php does require PHP programming skill. How much skill depends on what you want relay.cgi to do.

    If you feel you don't have the skill to add the code for the tasks you want accomplished, any person with sufficient skill can do it for you. I'm fairly good with most things programming and you are welcome to talk to me about what you need done.

  4. Upload relay.php to your server. Use its URL in the action values of forms whose submissions are to be relayed through 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 to relay.php, the script does its tasks and then sends a self-submitting form to the browser. The self-submitting form submits itself to the final destination.

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