这是连接到MySQL数据库并执行CRUD操作的PHP文件的一部分,'echo'内部的'alert'函数未执行..为什么?:
WebHost.CreateDefaultBuilder
答案 0 :(得分:2)
这是因为标头('location:index.php'); According to PHP:
请记住,必须先通过正常的HTML标签,文件中的空白行或从PHP发送任何实际输出,然后才能调用header()。
答案 1 :(得分:1)
不要回显js,只需关闭php标签并按原样编写
<?php
if (isset($_GET['del'])) {
$id = $_GET['del'];
?>
<script>
alert("The record <?=$id?> will be deleted");
</script>
<?php
mysqli_query($db, "DELETE FROM info WHERE id=$id");
$_SESSION['message'] = "Address deleted!";
header('location: index.php');
}
?>
答案 2 :(得分:0)
对此不太确定,但我认为分号(;)告诉echo在此停止,因此echo只打开了脚本,但看不到其余的内容
尝试一下:
if (isset($_GET['del'])) {
$id = $_GET['del'];
echo '<script language="javascript">
alert("The record"' . $id .'"will be deleted")
</script>';
// exit;
mysqli_query($db, "DELETE FROM info WHERE id=$id");
$_SESSION['message'] = "Address deleted!";
header('location: index.php');
}
答案 3 :(得分:0)
您需要像这样更改代码。
alert("The record " + '. $id .' +" will be deleted");
或者您也可以将引号转义,例如:
alert("The record \" '. $id .' \" will be deleted");
答案 4 :(得分:0)
警报的原因不是php函数,因此您需要在同一行中回显警报 通过这种方式 echo'alert(“记录”'。$ id。'“将被删除”)';
答案 5 :(得分:0)
您不能直接回显。试试这个
echo '<html><script> alert("The record"' . $id .'"will be deleted")<script></html>';
答案 6 :(得分:0)
先传回东西再重定向实际上是行不通的。 也许您想代替回显该消息,然后用户可以单击“确定”按钮以重定向到索引页面。
if (isset($_GET['del'])) {
$id = $_GET['del'];
mysqli_query($db, "DELETE FROM info WHERE id=$id");
$_SESSION['message'] = "Address deleted!";
echo '<script>';
echo 'function backToIndex() {';
echo 'location.replace("https://www.yoursite.com");';
echo '}';
echo '</script>';
echo '<div>';
echo '<p>The record ' . $id . ' is deleted.</p>';
echo '<button onclick="backToIndex()">ok</button>';
echo '</div>';
}
如果希望用户在删除前进行确认,则可以将时间戳记或随机字符串存储在会话变量中,并检查是否已设置。如果没有,请确认。如果已设置,请删除。
<?php
if (isset($_GET['del'])) {
$id = $_GET['del'];
if (!isset($_SESSION['del_confirmed'])) {
$_SESSION['del_confirmed'] = microtime();
}
if ($_GET['token'] === $_SESSION['del_confirmed']) {
mysqli_query($db, "DELETE FROM info WHERE id=$id");
$_SESSION['message'] = "Address deleted!";
unset($_SESSION['del_confirmed']);
header('location: index.php');
} else {
?>
<script>
function backToIndex() {
location.href= "index.php";
}
</script>
<div>
<p>Do you really want to delete record <?=$id?>?</p>
<form action="index.php" method="get">
<input type="text" name="del" value="<?=$id?>">
<input type="text" name="token" value="<?=$_SESSION['del_confirmed']?>">
<button type="submit">yes</button>
</form>
<button onclick="javascript:backToIndex();">no</button>
</div>
<?php
}
}
?>
答案 7 :(得分:0)
这将起作用。经过测试并为我工作。
if (isset($_GET['del'])) {
$id = $_GET['del'];
$alert = 'alert("The record' . $id .'will be deleted");';
echo "<script type='text/javascript'>";
echo $alert;
echo "</script>";
}
答案 8 :(得分:0)
尝试先关闭php,然后添加脚本标签。 我在网上试过了
<?php
if (isset($_GET['del'])) {
$id = $_GET['del'];
?>
<script>
alert("The record <?php echo $id ?> will be deleted");
</script>
<?php
mysqli_query($db, "DELETE FROM info WHERE id=$id");
$_SESSION['message'] = "Address deleted!";
header('location: index.php');
}
?>