gpt4 book ai didi

javascript - 在 PhP 中运行 javascript 警报

转载 作者:行者123 更新时间:2023-11-28 01:40:44 27 4
gpt4 key购买 nike

我一直想发布一个 javascript 警报(一个带有消息的弹出窗口)来简单地说“谢谢!您已被添加!”。用户在表单中输入详细信息并单击提交按钮后。

我已经在谷歌上搜索了如何执行此操作,但是在添加它时,它从未奏效并且尝试了不同的方法,但我现在对如何执行此操作感到困惑。如果有人知道我做错了什么,那么任何帮助将不胜感激。

我还想在一个单独的 php 文件中注明这一点。

下面是我的代码;

插入.php

<?php
$con=mysqli_connect("localhost","cl51-main-3i6","password","cl51-main-3i6");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$firstname = mysqli_real_escape_string($con, $_POST['firstname']);
$lastname = mysqli_real_escape_string($con, $_POST['lastname']);
$email = mysqli_real_escape_string($con, $_POST['email']);

$sql="INSERT INTO emails ( Firstname, Lastname, Email) VALUES ( '$firstname', '$lastname', '$email')";

if (!mysqli_query($con,$sql)) {
die('Error : ' . mysql_error($con));
}

echo '<script language="javascript">';
echo 'alert("Thank you! You've now joined the e-mail club!")';
echo '</script>';

header("Location: http://isometricstudios.co.uk/news.html");
exit;

mysqli_close($con);
?>

最佳答案

问题是因为 header() 会立即执行重定向。使用 window.location<script>而不是 header(location)

<?php
$con=mysqli_connect("localhost","cl51-main-3i6","password","cl51-main-3i6");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$firstname = mysqli_real_escape_string($con, $_POST['firstname']);
$lastname = mysqli_real_escape_string($con, $_POST['lastname']);
$email = mysqli_real_escape_string($con, $_POST['email']);

$sql="INSERT INTO emails ( Firstname, Lastname, Email) VALUES ( '$firstname', '$lastname', '$email')";

if (!mysqli_query($con,$sql)) {
die('Error : ' . mysql_error($con));
}

echo '<script language="javascript">';
echo 'alert("Thank you! You've now joined the e-mail club!")';
echo 'window.location.href="http://isometricstudios.co.uk/news.html";';
echo '</script>';

exit;

mysqli_close($con);
?>

现在只有按下确定按钮时才会显示和重定向警报

关于javascript - 在 PhP 中运行 javascript 警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26245403/

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