简体   繁体   English

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

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

I am trying to echo out a div with the message "form submitted successfully" on the next page once a user has submitted a form. 我试图在用户提交表单后在下一页上回显div,并显示消息“表单提交成功”。

i am trying to do this by using $_GET['success'] but can not seem to get the message to display, can someone please point me in the right direction. 我正在尝试通过使用$ _GET ['success']来执行此操作,但似乎无法显示该消息,有人可以向我指出正确的方向。

code: 码:

submit_form.php: submit_form.php:

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

index.php: index.php文件:

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

you are setting the session with text message so all you need to do this is to echo the session variable on index.php page 您正在设置带有文本消息的会话,因此您所需要做的就是在index.php页面上回显会话变量

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

Also put session_start(); 还放session_start(); at the beginning of every page. 在每一页的开头。

As you're already setting a session variable, just echo that out in your index.php file 由于您已经设置了session变量,因此只需在index.php文件中将其echo

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

You are storing the value in the variable $_SESSION['success'] and passing $success which is undefined so the the error is thrown 您将值存储在变量$_SESSION['success']并传递未定义的$success ,因此会引发错误

Replace you code with this code: 用以下代码替换您的代码:

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");

And in index.php just echo success as: 而在index.php中,只需将成功回显为:

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

Hope this will help! 希望这会有所帮助!

There is not need to use first two line in you first page. 第一页中无需使用前两行。 Just use redirection as you have used it on your third line and display message on your next page "index.php" like this: 只需在第三行中使用重定向即可,并在下一页“ index.php”上显示消息,如下所示:

First page: 第一页:

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

Index.php page: Index.php页面:

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

The amount of errors in your code is huge: 您的代码中的错误数量巨大:

submit_form.php: 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: 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
?>

It should work 100%. 它应该工作100%。

First page: 第一页:

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

Index.php page: 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