dongmiao520892 2014-03-03 12:27
浏览 143
已采纳

如何提交PHP表单帖子并一步重定向到确认页面?

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

  • 写回答

4条回答 默认 最新

  • douchun1961 2014-03-03 12:39
    关注

    You could do the POST request yourself, using curl for instance.

    //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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄