[英]How can I submit a PHP form post and redirect to a confirmation page in one step?
我有一个PHP表单,我需要从其中提交POST数据到第三方URL并重定向到确认页面。
我无法控制第三方页面,并且在POST之后无法提供任何确认,因此需要将其添加到我的代码中。
这是更详细的过程:
www.example-site-1.com/form.php上的PHP表单,该表单使用基本的htmlspecialchars($ _ SERVER [“ PHP_SELF”]); 在重定向之前执行验证:
<?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>
我添加了会话以继续到下一页,但是也许我不需要这些。 另外,{header('Location:process.php'); 例如,在那里,但这确实是我坚持的目标; 我应该如何执行以下操作?
本质上,一旦有人单击“提交”,我就需要进行两件事(两次单击均会发生):
1)使用POST将表单数据提交到第三方站点(出于演示目的,www.example-site-2.com / submit.php)。 用户永远都不会看到该页面(这是一个纯白色的屏幕,没有向用户提供反馈)。
2)点击提交的用户应转到www.example-site-1.com/thanks.php上的“感谢/确认”页面
这可能吗?
如前所述,很遗憾,我无权编辑第三方站点,因此无法在该站点上发布POST后添加确认。
PHP解决方案将是很好的选择,或者JavaScript将是很好的选择。
任何帮助或想法,不胜感激。 谢谢
也许您可以使用cURL? 我认为这样可以解决问题
// Where to make our request (third party site)
$ch = curl_init($urlToThirdParty);
// Make the request
curl_exec($ch);
// Did anything went wrong=
if (curl_errno($ch)) {
die('Oops: ' . curl_error($ch));
} else {
// Make sure we got a 200 back (indicating success)
$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpStatusCode != 200) {
die('Oops. Status code: ' . $httpStatusCode);
}
}
curl_close($ch);
// If we made it this far, redirect to thank you page
header('Location: '.$thankYouURL);
希望这可以帮助!
如果您只是将用户发送到第三方站点(如您现在所做的那样),那么就不会让他们回来。
有两种方法,一种是使用ajax向第三方站点提交表单请求,获取响应,然后将用户重定向到您站点上的感谢页面。
或者,如果您不想使用javascript,则将表单提交到您的感谢页面,然后在其中显示一些php,以便在显示感谢内容之前将表单请求提交给第三方网站(使用curl)
我认为您应该做的是:
创建一个随机令牌(一个随机数)并将其保存在$_SESSION['user_token']
。
$_SESSION['user_token'] = random_number(); // random_number() is a custom function which generates a random number of some length
然后将表单返回的数据保存到数据库中。 您的表必须具有一个名为“ token_value”或“ user_token”的列。 当用户返回您的页面时,检查与数据库的会话:
$stmt = $db->prepare("SELECT * FROM form_data WHERE token_value = ?");
$stmt->bind_param("s", $_SESSION['user_token']);
// and the rest of DB-related operation
您可以自己执行POST请求,例如使用curl。
//set POST variables
$url = 'http://domain.com/get-post.php'; //URL of that site with a blank page
$fields = array( //your parameters here
'lname' => urlencode($last_name),
'fname' => urlencode($first_name),
'title' => urlencode($title),
'company' => urlencode($institution),
'age' => urlencode($age),
'email' => urlencode($email),
'phone' => urlencode($phone)
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.