确认框不显示并自动删除

时间:2014-05-15 07:08:06

标签: javascript php confirmation

请帮助我。我创建了一个删除过程,其中有一个确认框,询问用户他是否确定。  但确认框没有出现并自动删除数据,甚至没有重定向我。帮帮我吧!

我需要显示确认框(“你确定”)并且输入是否为真。它将删除数据并重定向到其他页面。

 <?php
 require_once('connect/connect.php');
 ?>
 <script>
 var r = confirm("Are you sure?");
 if (r == true)
 {
 alert(' item successfuly deleted!!');
 </script>
 <?php

 if(isset($_GET['pid'])){
$sql ='Select * FROM product where pid = '.$_GET['pid'];
$qry = mysql_query($sql);
$data = mysql_fetch_array($qry);
$pname = $data['pname'];
$pstock = $data['pstock'];
}
$sq3="INSERT INTO report VALUES('NULL','".$pname."', '" .$pstock. "', 'Item Deleted' )";
$qry3 = mysql_query($sq3);

$sql2 = 'DELETE FROM product where pid = '.$_GET['pid'];
$qry2 = mysql_query($sql2);
?>
<script>
 window.location.assign('products.php');    
 }
 </script>
 <script>
 else
 {
window.location.assign('products.php');
 }
</script>

3 个答案:

答案 0 :(得分:2)

你错过了}条件

的大括号if
 <script>
 var r = confirm("Are you sure?");
 if (r == true)
 {
 alert(' item successfuly deleted!!');
 }
 </script>

FIDDLE

答案 1 :(得分:0)

尝试简短的if声明

(r == true) ? alert('item successfuly deleted!!') : "" ;

答案 2 :(得分:0)

您必须在php标记开始之前删除脚本标记。更新代码如下: -

       <?php
       require_once('connect/connect.php');
       ?>
     <script>
    var r = confirm("Are you sure?");
    if (r == true)
     {
       alert(' item successfuly deleted!!');

       <?php

       if(isset($_GET['pid'])){
       $sql ='Select * FROM product where pid = '.$_GET['pid'];
       $qry = mysql_query($sql);
       $data = mysql_fetch_array($qry);
       $pname = $data['pname'];
       $pstock = $data['pstock'];
       }
       $sq3="INSERT INTO report VALUES('NULL','".$pname."', '" .$pstock. "','Item Deleted' )";
        $qry3 = mysql_query($sq3);

        $sql2 = 'DELETE FROM product where pid = '.$_GET['pid'];
        $qry2 = mysql_query($sql2);
        ?>

         window.location.assign('products.php');    
        }

       else
       {
         window.location.assign('products.php');
        }
      </script>

试试并确认。