2013-10-10 45 views

回答

0

您可以使用cUrl發送所需的POST數據,然後進行重定向。

在網上查找:「php curl」。

0

讓您form action點要執行的功能和PHP文件那麼到底這個地方header("location: your_location_file.php");

第一步 - 提交表單的functions.php
第二步 - 你需要什麼都做的submited數據
步驟三 - 重定向

實施例:

<form method="post" action="functions.php"> 
... 
</form> 

functions.ph p

<?php 
... 
all your code 
... 
header("location: your_location_file.php"); 
?> 
+0

這很好,但他希望將發佈的數據轉移到your_location_file.php。 – idmean

0

如果您不想依靠捲曲,Javascript可以提供幫助。有這個鋪設。傳入$ _POST或您想要發佈的數據的數組。添加錯誤/參數檢查。

function http_post_redirect($url='', $data=array(), $doc=true) { 

    $data = json_encode($data); 

    if($doc) { echo "<html><head></head><body>"; } 

    echo " 
    <script type='text/javascript'> 
     var data = eval('(' + '$data' + ')'); 
     var jsForm = document.createElement('form'); 

     jsForm.method = 'post'; 
     jsForm.action = '$url'; 

     for (var name in data) { 
      var jsInput = document.createElement('input'); 
      jsInput.setAttribute('type', 'hidden'); 
      jsInput.setAttribute('name', name); 
      jsInput.setAttribute('value', data[name]); 
      jsForm.appendChild(jsInput); 
     } 
     document.body.appendChild(jsForm); 
     jsForm.submit(); 
    </script>"; 

    if($doc) { echo "</body></html>"; } 
    exit; 
} 
+0

謝謝你的回答。數組'$ data'看起來應該如何?用這個:'$ data = array( 「year」=>「2013」​​, 「month」=>「3」 );'它不工作。它的重定向,但POST數據不是submited – Mike

+0

必須複製一箇舊的,對不起。編輯。 – AbraCadaver

0

您可以使用會話來保存POST數據。

我目前使用的代碼如下。在我的第一頁加載時,$ _POST數據被檢查。如果它包含數據庫中已有的某些值,則它將重定向到這些值的頁面。

// This could be part of the same script as below, or a different script. 
session_start(); 

if($_POST['my_value'] && valueExistsInMyDb($_POST['my_value'])) { // check my db to see if this is an existing value 

    $id = getIdOfMyValue($_POST['my_value']); // e.g. '4' 

    $_SESSION['POST'] = $_POST; // take ALL post data and save it in the session variable 

    header("location: your.php?myvalue=" . $id); // redirect to bookmarkable target page where $_GET variable matches what was posted. 
    exit(); // ensure no other code is executed in this script after header is issued. 
} 

然後你的其他文件(或者甚至同一文件)可以這樣做:

// your.php?myvalue=4 

if(isset($_SESSION) && array_key_exists('POST',$_SESSION)) { 
    $_POST = $_SESSION['POST']; // creates or overwrites your $_POST array with data from the session. The rest of your script won't be able to tell that it's not a real $_POST, which may or may not be what you want. 
    unset($_SESSION['POST']); // you probably want to remove the data from the session. 
} 
// now your myvalue=4 is stored in GET, and you can handle the rest of the POST data as you like 

我不知道這是否是最好的解決方案,但到目前爲止,這似乎是工作對我來說迄今爲止。我只是在前幾天寫了代碼,並沒有測試所有方面。

另一種選擇是使用HTML5來更改地址欄。不需要重定向。但缺點是隻有「現代Webkit瀏覽器」可以使用它,顯然。