在使用复选框删除数据之前,我希望显示确认消息


I want show the confirmation message before deleting a data using checkbox

在删除数据之前,我在显示确认消息时遇到问题

if(isset($_POST['del'])){
        echo "
            <script>
                var x;
                    x = confirm ('You want to proceed deleting?');
                    if(x == true){";
            for($i=0;$i<$_SESSION['count'];$i++){
            $checkbox = $_POST['checkbox'];
            $del_id = $checkbox[$i];
            $query = mysql_query("SELECT picture FROM tblreserve WHERE id='$del_id'");
            while($row = mysql_fetch_assoc($query)){
                $del_pic = $row['picture'];
            }
            $sql = "DELETE FROM tblreserve WHERE id = $del_id";
            $result = mysql_query($sql);
            if($result){
                if(is_file($del_pic)){
                        unlink($del_pic);
            }
    }
    }
    header('location:view.php');
                    echo "}
                    else{
                        location.href = 'view.php';
                        }
                    </script>";
    }

您不能使用JavaScript来控制PHP的输出,因为PHP是在服务器端执行的,而JavaScript是由客户端执行的,但是您可以使用JavaScript将确认集成到删除按钮中,如下例所示:

<button onclick="if (confirm('Are you sure you want to delete?')) window.location = 'some_other_page.php'">Delete</button>

当浏览器中出现确认框时,所有php代码都会在文件中解析。在mysql中删除数据是通过php,所以你想通过点击javascript提示按钮来确认这一点,你应该通过从url发送一个标签来刷新你的页面,或者你可以通过ajax来实现它。在给定的代码段中,php不能被javascript confirm停止。您应该知道一个是服务器端,另一个是客户端

相关文章: