HELP ME Please. I created a delete process wherein there i a confirm box to ask the user if he is sure or not. but the confirm box doesnt appear and automatically deletes the data and doesnt even redirect me. Help me plss!

I need to show the confirmation box ("are u sure") and if the input is true. it will delete the data and redirect to other page.

 <?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>
有帮助吗?

解决方案

you missed the closing brace } of if condition

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

FIDDLE

其他提示

Try a short if statement

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

You have to remove the script tags before php tags start. Update the code as below:-

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

Try this and confirm.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top