2013-10-03 71 views
-1

我想在數據庫中添加一些數據,插入後我想要一個警報,但它不工作,除了該頭()函數工作。javascript警報()不工作

<?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

** **危險:您正在使用[**的**過時的數據庫API(http://stackoverflow.com/q/12859942/19068),並應使用[現代替代](http://php.net/manual/en/mysqlinfo.api.choosing.php)。你也**易受[SQL注入攻擊](http://bobby-tables.com/)**,現代的API會使[防禦]更容易(http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php)自己從。 – Quentin

+0

添加上面的事實,這是希望成爲一個銀行網站... –

+0

「閉嘴,拿我的錢」 - 銀行 –

回答

2

這很容易:你有Location頭:

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

當您發送Location頭給用戶,他/她將被重定向和腳本不會被解釋!

P.S:正如@Quentin所說,mysql_函數已被棄用,它們已老,不再維護。使用mysqliPDO。第2頁:您在標題前發送內容,因此假設您使用output buffering

+0

你的意思是'mysql_ *'函數當然不推薦使用。 –

+0

@Sébastien對不起,人工修改。 – undone

+0

是的,我得到了謝謝@undone –

0

你在echo之後的標題處消失,但應該給出一個錯誤(如果輸出(如回顯)已經放在屏幕上,你不能header())。

我asume你使用類似ob_start()
我建議以下方法:

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

如果是這樣,在balking.php檢查isset($_GET['alert']),回聲,如果不是,不要做任何事情

+0

但標題工作..我剛試過你的方法和itssssssssssssssssssssss工作......謝謝 –

+0

請標記爲其他人閱讀本主題的解決方案:) – Martijn

+0

其實我需要知道如何標記答案作爲解決方案。 –

0

header(「Location:banking.php」); JavaScript的運行 使用<script>window.location.replace("http://mydomain.com/banking.php");</script>

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