简体   繁体   English

在提交表单之后但在重定向和退出之前回显成功消息

[英]Echo success message after submitted form but before redirect and exit

Don't know how long i have worked on this, but nothing looks to be working. 不知道我在这方面做了多长时间,但看起来没有任何效果。 All i want to do is output a success message before it redirects and exits. 我想要做的就是在重定向和退出之前输出成功消息。

This is what i got to check for success/fail to the database 这是我检查数据库成功/失败的原因

$sql = ("INSERT INTO test3 (test1, betvalue, bookmaker, status, aktiv) VALUES ('$bettype', '$betvalue', '$bookmaker', '$status', '$aktiv')");

    $result = mysqli_query($conn, $sql);
    if(mysqli_affected_rows($conn) >0 ) {
      echo "Wow it works";                    <-- Not working
      header("Location: nybonus.php");
      exit;
    } else {
      echo ("<div class='alert alert-danger' role='alert'>" . $sql . "</div><div class='alert alert-danger' role='alert'>" . mysqli_error($conn) . "</div>");
    }

It does not output any echo if it has be successful, i have tried making it sleep with correct flushing to try to output an messsage, but it does not work at all. 如果它成功,它不会输出任何回声,我已经尝试使用正确的刷新进行睡眠以尝试输出消息,但它根本不起作用。 Is there a better way to do it? 有没有更好的方法呢? Also can someone explain why it wont echo before the header and exit? 也有人可以解释为什么它不会在标题和退出之前回显?

The problem is when you use the "header" it has to be first. 问题是当你使用“标题”时它必须是第一个。 You can't print anything else to the screen before the header. 您无法在标题之前将任何其他内容打印到屏幕上。 It's the same as when you're setting cookies. 这与您设置cookie时的情况相同。

So in your case you can't do the redirect with "header". 因此,在您的情况下,您无法使用“标题”进行重定向。 You could use a meta refresh. 您可以使用元刷新。

<meta http-equiv="refresh" content="0; url=http://www.somewebsite.com">

The "content" is number of seconds to wait before doing refresh. “内容”是刷新前等待的秒数。

Could you achieve what you want by setting a session variable and checking for the state of that variable on reload: 您可以通过设置会话变量并在重新加载时检查该变量的状态来实现您想要的效果:

$message = '';
if(isset($_SESSION['success'])) {
    if($_SESSION['success'] == 'success') {
        $message = 'Success';
    }
}

$sql = ("INSERT INTO test3 (test1, betvalue, bookmaker, status, aktiv) VALUES ('$bettype', '$betvalue', '$bookmaker', '$status', '$aktiv')");
mysqli_query($conn, $sql);
if(mysqli_affected_rows($conn) > 0) {
    $_SESSION['success'] = 'success';             
    header('Location: nybonus.php');
    exit;
}
else {
    $_SESSION['success'] = 'fail';
    ..... 
}

<script>
$("#message").fadeOut(2000, function() {
    $("#message").fadeIn(0, function() {
        document.getElementById("message").innerHTML ="<br>";

    });
});
</script>

<div id="message">
    <?php echo $message; ?>
</div>

You'll have to explain what you're trying to accomplish more precisely... like step 1 do this step 2 do this and so on. 你必须更准确地解释你想要完成的事情......比如第1步执行此步骤2执行此操作,依此类推。 Previously I understood you had asked how to do a redirect after echoing a message. 以前我知道你曾经问过如何在回复消息后进行重定向。

So now explain more detail in steps what you're wanting to do and maybe I can help. 所以现在解释一下你想要做的更多细节,也许我可以提供帮助。

If you mean the user is one "page1" and they submit a form and then on the resulting page you want to display the message? 如果您的意思是用户是一个“page1”并且他们提交了一个表单,然后在结果页面上显示该消息? Then redirect... 然后重定向...

You would just need to make the form action be the same url path as the page they are currently on. 您只需要使表单操作与他们当前所在页面的URL路径相同。

Do you follow me? 你听懂了吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM