-1

I am trying to add some data in the database , after insertion i want an alert but it doesn't working besides that header() function is working.

<?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");
            }
        }
    ?>
  • 3
    **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Oct 03 '13 at 10:22
  • add the above to the fact that this is looking to become a BANKING site... – STT LCU Oct 03 '13 at 10:25
  • "Shut up and take my money"-bank – Daniele Vrut Oct 03 '13 at 10:27
  • its for a shop management system, where banking is a module of this system @STTLCU – Forhad Sikder Oct 05 '13 at 09:13
  • thanks for the suggesstion @Quentin – Forhad Sikder Oct 05 '13 at 09:14

5 Answers5

2

It's easy: you have Location header:

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

When you send Location header to user, he/she will get redirected and script won't be interpreted!

P.S: As @Quentin said, mysql_ functions are deprecated, they're old and no longer maintained. Use mysqli or PDO.

P.S 2: You send content before header, so assume you're using output buffering.

undone
  • 7,857
  • 4
  • 44
  • 69
1

You header away right after the echo, but that should give an error (you can not header() if output (like echo) has been put on the screen).

I asume you use something like ob_start()
I suggest the following method:

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

and check in balking.php for isset($_GET['alert']), if so, echo it, if not, dont do anything

Martijn
  • 15,791
  • 4
  • 36
  • 68
0

header("Location: banking.php"); redirect you before javascript run use <script>window.location.replace("http://mydomain.com/banking.php");</script>

Sergey Karasev
  • 4,513
  • 4
  • 25
  • 24
0

Try this...

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

Try

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

it terminates running php and executes javascript