不知道我已经花了多长时间了,但是似乎什么也没有。我要做的就是在重定向和退出之前输出一条成功消息。
这就是我要检查数据库成功/失败的条件
$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>");
}
如果成功,它不会输出任何回声,我曾尝试使其以正确的冲洗进入睡眠状态,以尝试输出消息,但它根本不起作用。 有更好的方法吗?还可以解释为什么它在标题和出口之前不会回显吗?
答案 0 :(得分:2)
问题是,当您使用“标题”时,它必须是第一个。 您无法在标题之前在屏幕上打印其他任何内容。 与设置Cookie时相同。
因此,在您的情况下,您无法使用“标头”进行重定向。 您可以使用元刷新。
<meta http-equiv="refresh" content="0; url=http://www.somewebsite.com">
“内容”是刷新之前要等待的秒数。
答案 1 :(得分:1)
您可以通过设置会话变量并在重新加载时检查该变量的状态来实现所需的功能吗?
$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>
答案 2 :(得分:0)
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. 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? Then redirect...
You would just need to make the form action be the same url path as the page they are currently on.
Do you follow me?