gpt4 book ai didi

javascript - sql完成后重定向并弹出

转载 作者:太空宇宙 更新时间:2023-11-04 16:13:51 24 4
gpt4 key购买 nike

我想在删除项目时让我的应用程序弹出一条消息并重定向到另一个页面。我使用 javascipt 作为弹出窗口,使用 php header 进行重定向。现在它唯一做的就是弹出窗口或重定向,具体取决于首先列出的一个。我该如何解决这个问题?

<?php
session_start();
require_once('../../includes/mysql_config.php');

$id = isset($_SESSION['id']) ? $_SESSION['id'] : header('location: ../../login.php');
$Cursist = mysqli_query($con, "SELECT id FROM users WHERE id =".$_SESSION['id']);
if(!$Cursist){
header('location: ../login.php');
}

$test = $_GET['id'];

$sql = "DELETE FROM cursus WHERE id = $test";
$result = mysqli_query($con, $sql);
if ($result) {
echo "<script type='text/javascript'>alert('Verwijdert!')</script>";
header("Location: ../cursussen.php?destoyed=true&destroyed_id=".$_GET['id']);
}else {
echo "mislukt";
}
?>

最佳答案

如果您在 header 之前发送一些内容将不起作用。在向客户端发送某些内容之前,您只能使用 header 。

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

http://php.net/manual/en/function.header.php

您可以使用 javascript,但不建议这样做,因为用户可能禁用 javascript:

echo "<script type='text/javascript'>";
echo "alert('Verwijdert!')";
echo "document.location.href='index.html'";
echo "</script>";

最好的方法是使用session和header,你可以在session中保存一个var,并在var为true时显示一条消息,当你显示消息时删除session var

删除.php

$_SESSION['deleted'] = true;
header("Location: index.php);

index.php

<?php if($_SESSION['deleted']){ ?>
<?php unset($_SESSION['deleted']) ?>
<div>Item was deleted</div>
<?php } ?>

关于javascript - sql完成后重定向并弹出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41227573/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com