PHP,提交表单并使用标题重定向并回显成功消息?

时间:2014-04-10 10:46:23

标签: php session

我正在尝试用消息"表格提交成功回复一个div"一旦用户提交了表单,就在下一页上。

我试图通过使用$ _GET ['成功']来做到这一点,但似乎无法显示消息,有人可以指出我正确的方向。

代码:

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'] ?>

6 个答案:

答案 0 :(得分:1)

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();放在每页的开头。

答案 1 :(得分:0)

由于您已经设置了session变量,只需在index.php文件中设置echo

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

答案 2 :(得分:0)

您将值存储在变量$_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!'; 
  ?>

希望这会有所帮助!

答案 3 :(得分:0)

您的第一页无需使用前两行。只需使用重定向,就像在第三行使用它一样,并在下一页显示消息&#34; index.php&#34;像这样:

第一页:

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

Index.php页面:

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

答案 4 :(得分:0)

代码中的错误数量巨大:

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
?>

答案 5 :(得分:0)

它应该100%工作。

第一页:

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

Index.php页面:

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