[英]Using previously filled out HTML form to submit that same form again with same values using PHP only?
我有兩個文件。
像這樣的 HTML 形式:
<form action="resubmitWithPHP.php" method="POST">
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname"><br><br>
<input type="submit" name=submitButton value="Submit">
</form>
用戶輸入他們的詳細信息並在單擊提交后轉移到“resubmitWithPHP.php”
這個腳本正在等待的地方是:
<?php
if(isset($_POST['submitButton']))
{
$firstName = $_POST['fname'];
$lastName = $_POST['lname'];
?>
<form action="otherPage.php" method="POST">
<label for="fname">First name:</label>
<input type="hidden" id="fname" name="fname" value="<?php echo $firstName; ?>"><br><br>
<label for="lname">Last name:</label>
<input type="hidden" id="lname" name="lname" value="<?php echo $lastName; ?>"><br><br>
<input type="submit" name=submitButton value="Submit">
</form>
<?php
}
else
{
header("Location: ../goBack.php?sent=nope");
}
?>
我的問題是,如何在沒有用戶進一步交互的情況下提交這個新表單? 我希望頁面盡快自動提交表單。 最好不加載或顯示任何內容。
您不需要表單或為此提交,您可以通過發送 POST 請求使用 php 執行此操作。 檢查一下: 如何使用 PHP 發送 POST 請求? , 由dbau回答
所以你想要的可能是這樣的。
<?php
if(isset($_POST['submitButton']))
{
$firstName = $_POST['fname'];
$lastName = $_POST['lname'];
$url = "http://localhost/otherPage.php";
$data = ['fname' => $firstName, 'lname' => $lastName];
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
}
else
{
header("Location: ../goBack.php?sent=nope");
}
?>
希望這會有所幫助,最好的問候
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.