I have a PHP form, from which I need to submit POST data to a third-party URL and redirect to a confirmation page.
I have no control over the third-party page and that provides no confirmation after POST, so I need to add that into my code.
Here is the process in more detail:
PHP form on www.example-site-1.com/form.php, which uses basic htmlspecialchars($_SERVER["PHP_SELF"]); to perform validation, before redirecting:
<?php
// Setup Empty Fields
$first_name = $last_name = $job_title = "";
// Setup Empty Error Messages
$first_nameError = $last_nameError = $job_titleError = "";
// Validate Field Entry
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$valid = true; // Redirect if valid
if (empty($_POST["first_name"]))
{$valid = false; $first_nameError = "Provide first name";}
else
{$first_name = htmlspecialchars($_POST["first_name"]);}
if (empty($_POST["last_name"]))
{$valid = false; $last_nameError = "Provide surname";}
else
{$last_name = htmlspecialchars($_POST["last_name"]);}
if (empty($_POST["job_title"]))
{$valid = false; $job_titleError = "Provide your job title";}
else
{$job_title = htmlspecialchars($_POST["job_title"]);}
// Start session
session_start();
// Register session
session_register('first_name');
session_register('last_name');
session_register('job_title');
// Populate
$_SESSION['first_name'] = $first_name;
$_SESSION['last_name'] = $last_name;
$_SESSION['job_title'] = $job_title;
// Redirect valid form to process
if($valid)
{header('Location: process.php');
exit();
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body>
<div>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" name="submit_data" method="POST" id="submit_data">
<fieldset>
<legend>Your Details</legend>
<p><label for="first_name">First Name</label><input type="text" id="first_name" size="20" maxlength="50" value="<?php echo $first_name;?>" name="[first_name]" /><br /><span class="error"> <?php echo $first_nameError;?></span></p>
<p><label for="last_name">Surname</label><input type="text" id="last_name" size="20" maxlength="50" value="<?php echo $last_name;?>" name="[last_name]" /><br /><span class="error"> <?php echo $last_nameError;?></span></p>
<p><label for="job_title">Job Title</label><input type="text" id="job_title" size="30" maxlength="30" value="<?php echo $job_title;?>" name="[job_title]" /><br /><span class="error"> <?php echo $job_titleError;?></span></p>
</fieldset>
<input type="submit" value="Submit" />
</form>
</div>
</body>
</html>
I've added sessions to carry over into the subsequent page, but perhaps I don't need these. Also, the {header('Location: process.php'); is there for example, but that is really what I am stuck on; how should I perform the following?
Essentially, once somebody clicks Submit, I need 2 things to happen (both from the one click):
1) Form data is submitted to a third-party site (for demo purposes, www.example-site-2.com/submit.php) using POST. User should never see that page (it is a plain white screen, with no feedback provided to the user).
2) User that clicked submit should be transferred to a thank you/confirmation page on www.example-site-1.com/thanks.php
Is this possible?
As mentioned, I unfortunately have no access to edit the third-party site, so cannot add a confirmation after POST on that site.
PHP solution would be great, or JavaScript would be fine.
Any help or ideas much appreciated. Thanks