javascript alert()无效

时间:2013-10-03 10:20:50

标签: javascript php

我正在尝试在数据库中添加一些数据,插入后我想要一个警报,但除了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");
            }
        }
    ?>

5 个答案:

答案 0 :(得分:2)

很简单:你有位置标题:

 echo "<script>alert('Addition Successfull !!');</script>";
 header("Location: banking.php");

当您向用户发送位置标题时,他/她将被重定向并且不会解释脚本!

P.S:正如@Quentin所说, mysql _ 函数已被弃用,它们已经过时且不再维护。使用 mysqli PDO

P.S 2:您在标题之前发送内容,因此假设您正在使用output buffering

答案 1 :(得分:0)

你在回声之后立即向前倾斜,但这应该给出一个错误(如果输出(如回声)已放在屏幕上,则不能header()。)

我认为你使用类似ob_start()的东西 我建议使用以下方法:

header("Location: banking.php?alert=Addition%20Successfull%20!!");

并在balking.php中查看isset($_GET['alert']),如果是,请回复它,如果没有,请不要做任何事情

答案 2 :(得分:0)

header(“Location:banking.php”);在javascript运行之前重定向你 使用<script>window.location.replace("http://mydomain.com/banking.php");</script>

答案 3 :(得分:0)

试试这个......

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");
        }

答案 4 :(得分:-1)

尝试

echo "<script>alert('Addition Failed !!');window.location='index.php';</script>";exit;

它终止运行php并执行javascript