>我正在尝试在数据库中添加一些数据,插入后我想要一个警报,但除了 header() 函数正常工作之外它不起作用。
<?php
include("connection.php");
if(isset($_POST['submit_bank']))
{
$b_date=$_POST['bank_date'];
$b_type=$_POST['bank_type'];
$b_name=$_POST['bank_name'];
$b_bname=$_POST['bank_bname'];
$b_amount=$_POST['bank_amount'];
$result = mysql_query("INSERT INTO banking_info(b_date,b_type,b_name,b_branch,b_amount)
VALUES('$b_date','$b_type','$b_name','$b_bname','$b_amount')");
if ($result!=0)
{
echo "<script>alert('Addition Successfull !!');</script>";
header("Location: banking.php");
}
else
{
echo "<script>alert('Addition Failed !!');</script>";
header("Location: banking.php");
}
}
?>
这很简单:你有位置标题:
echo "<script>alert('Addition Successfull !!');</script>";
header("Location: banking.php");
当您向用户发送位置标头时,他/她将被重定向,并且脚本不会被解释!
PS:正如@Quentin所说,mysql_函数被弃用,但它们是旧的,不再维护。使用 mysqli 或 PDO。
P.S 2:您在标头之前发送内容,因此假设您使用的是输出缓冲。
你在回声之后立即离开,但这应该会给出一个错误(如果输出(如回声)已被放在屏幕上,你无法header()
)。
我建议你用类似ob_start()
我建议以下方法:
header("Location: banking.php?alert=Addition%20Successfull%20!!");
并检查犹豫不决.php对于isset($_GET['alert'])
,如果是这样,请回显它,如果没有,则不要做任何事情
header("Location: banking.php"); 在 JavaScript 运行之前重定向您使用 <script>window.location.replace("http://mydomain.com/banking.php");</script>
<</p>
试试这个...
if ($result!=0)
{
echo "<script>
alert('Addition Successfull !!');
window.location.replace('banking.php');
</script>";
//header("Location: banking.php");
}
else
{
echo "<script>
alert('Addition Failed !!');
window.location.replace('banking.php');
</script>";
//***strong text***header("Location: banking.php");
}
<</div>
div class="answers"> 尝试
echo "<script>alert('Addition Failed !!');window.location='index.php';</script>";exit;
它终止运行PHP并执行JavaScript