PHP - 通过POST方式重定向并发送数据

51
我有一个在线网关,需要提交带有隐藏字段的HTML表单。我需要通过PHP脚本完成此操作,而无需任何HTML表单(我在数据库中拥有隐藏字段的数据)。
要使用GET方法发送数据:
header('Location: http://www.provider.com/process.jsp?id=12345&name=John');

那么要通过POST方法发送数据应该怎么做呢?

14个回答

0

虽然这是一篇旧帖子,但我来分享一下我的处理方法。使用newms87的方法:

if($action == "redemption")
{
    if($redemptionId != "")
    {
        $results = json_decode($rewards->redeemPoints($redemptionId));

        if($results->success == true)
        {
            $redirectLocation = $GLOBALS['BASE_URL'] . 'rewards.phtml?a=redemptionComplete';
            // put results in session and redirect back to same page passing an action paraameter
            $_SESSION['post_data'] = json_encode($results);
            header("Location:" . $redirectLocation);
            exit();
        }
    }
}
elseif($action == "redemptionComplete")
{
    // if data is in session pull it and unset it.
    if(isset($_SESSION['post_data']))
    {
        $results = json_decode($_SESSION['post_data']);
        unset($_SESSION['post_data']);
    }
    // if you got here, you completed the redemption and reloaded the confirmation page. So redirect back to rewards.phtml page.
    else
    {
        $redirectLocation = $GLOBALS['BASE_URL'] . 'rewards.phtml';
        header("Location:" . $redirectLocation);
    }
}

0

是的,你可以在PHP中做到这一点,例如在

Silex或Symfony3

使用子请求(subrequest)

$postParams = array(
    'email' => $request->get('email'),
    'agree_terms' => $request->get('agree_terms'),
);

$subRequest = Request::create('/register', 'POST', $postParams);
return $app->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);

0
我使用了以下代码来捕获从form.php提交的POST数据,然后将其连接到一个URL上发送回表单进行验证和更正。这个方法非常有效,实际上将POST数据转换为GET数据。
foreach($_POST as $key => $value) {
   $urlArray[] =  $key."=".$value;  
}
$urlString = implode("&", $urlArray);

echo "Please <a href='form.php?".$urlString."'>go back</a>";

很遗憾,这不是被要求的内容。他想通过重定向进行POST,而不是将POST转换为GET。 - TimWolla
1
此外,如果你想实现这个功能,你也可以使用PHP的原生函数http_build_query() - Ben Fransen

0
一个完美的解决方法:
在源页面中,开始打开一个会话并分配尽可能多的值。 然后使用“header”进行重定向:
<!DOCTYPE html>
<html>
   <head>
       <?php
           session_start();
           $_SESSION['val1'] = val1;
           ...
           $_SESSION['valn'] = valn;
           header('Location: http//Page-to-redirect-to');
       ?>
   </head>
</html>

然后,在目标页面中:

<!DOCTYPE html>
<?php
    session_start();
?>
<html>
    ...
    <body>
        <?php
            if (isset($_SESSION['val1']) && ... && isset($_SESSION['valn'])) {
                YOUR CODE HERE based on $_SESSION['val1']...$_SESSION['valn'] values
            }
        ?>
    </body>
</html>

不需要JavaScript或JQuery...祝你好运!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接