簡體   English   中英

PHP,使用標題提交表單並重定向並回顯成功消息?

[英]PHP, submit form and redirect using header and echo out a success message?

我試圖在用戶提交表單后在下一頁上回顯div,並顯示消息“表單提交成功”。

我正在嘗試通過使用$ _GET ['success']來執行此操作,但似乎無法顯示該消息,有人可以向我指出正確的方向。

碼:

submit_form.php:

session_start();
$_SESSION['success'] = "<div class='success'>Form Submitted Successfully</div>;
header("Location: index.php?success=$success");

index.php文件:

<?php
$_GET['success'] ?>
session_start();
$_SESSION['success'] = "<div class='success'>Form Submitted Successfully</div>;
header("Location: index.php");

您正在設置帶有文本消息的會話,因此您所需要做的就是在index.php頁面上回顯會話變量

<?php
    if(isset($_SESSION['success']))
    {
        echo $_SESSION['success'];
    }
  ?>

還放session_start(); 在每一頁的開頭。

由於您已經設置了session變量,因此只需在index.php文件中將其echo

session_start();    
<?php echo $_SESSION['success']; ?>

您將值存儲在變量$_SESSION['success']並傳遞未定義的$success ,因此會引發錯誤

用以下代碼替換您的代碼:

session_start();
$success = "<div class='success'>Form Submitted Successfully</div>"; // use the $success
//encode the URL parameter as : 

$success = urlencode($success);

header("Location: index.php?success=$success");

而在index.php中,只需將成功回顯為:

 <?php 
    echo isset($_GET['success'])?$_GET['success']:'Error!'; 
  ?>

希望這會有所幫助!

第一頁中無需使用前兩行。 只需在第三行中使用重定向即可,並在下一頁“ index.php”上顯示消息,如下所示:

第一頁:

header("Location: index.php?success=done");

Index.php頁面:

if(isset($_REQUEST['success'])=="done")
{
    echo "<div class='success'>Form Submitted Successfully</div>";
}

您的代碼中的錯誤數量巨大:

submit_form.php:

<?php
session_start();
$_SESSION['success'] = "<div class='success'>Form Submitted Successfully</div>"; // added " at the end
header("Location: index.php?success"); //Do NOT pass HTML in here. That makes an XSS security vulnerability 
?>

index.php文件:

<?php
session_start(); //ALWAYS start with session_start if you use sessions. session_start should be at the very beginning of your page.
if(isset($_GET['success']))
    echo $_SESSION['success']; //you put your data in _SESSION, you echo it from _SESSION
?>

它應該工作100%。

第一頁:

header("Location: index.php?success=done");

Index.php頁面:

if(isset($_REQUEST['success'])=="done")
{
    echo "<div class='success'>Form Submitted Successfully</div>";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM