使用php var确认删除

时间:2015-05-20 08:51:08

标签: javascript php

我的代码示例:

    <form action="test.php" method="GET">

        <input type="text" name="Delete_This" value="<?php if (isset($_GET['Delete_This'])) echo $_GET['Delete_This']?>">
        <input type="submit" name="Deleting" value="Delete this">

        <?php
            // If the deleting button is pressed
            if (isset($_GET['Deleting']))
            {

                $DeleteThis = $_GET['Delete_This'];

               ?> 
                <script type='text/javascript'>
                    if (confirm('Are you sure you want to delete <?php echo $DeleteThis ?> ?'))
                        alert('<?php echo $DeleteThis ?> deleted');
                         <?php
                           $sql_delete="DELETE FROM table WHERE Name like '".$DeleteThis ."'";
                           mysqli_query($db,$sql_delete);
                        ?>
                   else
                    alert('Not deleted');


                </script>               
        <?php } ?>
    </form>

确认框确实打开。我的问题是,当我点击确定取消时,会执行$sql_delet。 我知道我应该在我的文档的顶部<head>使用Javascript,但如果我这样做,我就无法执行删除查询,我必须警告用户他要删除的内容。 {$DeleteThis}工作正常。我还使用onclick上的input函数,但我无法使用{$DeleteThis}

要合成:我希望,如果confirm框被取消,查询将不被执行,我必须在confirmalert中使用php变量。

3 个答案:

答案 0 :(得分:3)

您可能想要了解服务器端语言和客户端语言之间的区别。

您在PHP代码中输入的所有内容都会在html和javascript代码发送到用户浏览器之前执行。

相比之下,你在javascript代码中放置的所有东西都不再能影响php代码。

那么代码中发生的是php首先检查是否有$_GET['Deleting']变量。如果有的话,它会找到一些html和javascript,并记住它应该稍后输出到浏览器。然后它执行删除查询。一旦完成,它会将以下javascript(以及任何周围的html)发送到浏览器:

<script type='text/javascript'>
    if (confirm('Are you sure you want to delete your-deletion-value ? \nHey'))
        alert('your-deletion-value deleted');

   else
    alert('Not deleted');


</script>

所以希望你能看到两者之间的脱节。

答案 1 :(得分:1)

你可以把确认放在

if (isset($_GET['Deleting']))
        {

            $DeleteThis = $_GET['Delete_This'];
            if(!isset($_GET['ConfirmDelete'])//if confirmation has not been initiated yet
            {
           ?> 

            <script type='text/javascript'>
            function deleteOrNot(formEle)
            {
                if (confirm('Are you sure you want to delete <?php echo $DeleteThis ?> ?'))
                    window.location += '&ConfirmDelete=yes';//casue a postback with ConfirmDelete attribute in $_GET

               else
                alert('Not deleted');
            </script>               
    <?php }
          else if($_GET['ConfirmDelete'] == 'yes')
             {
                 $sql_delete="DELETE FROM table WHERE Name like '".$DeleteThis ."'";
                 mysqli_query($db,$sql_delete);
             }
 } ?>

或者您可以在表单中使用带有必需属性的复选框,然后设置提交按钮以设置复选框的值,然后再使用onclick提交。

<input type="checkbox" name='ConfirmDelete' 
    required <?php if(isset($_GET['ConfirmDelete']))  
        echo 'checked=' . $_GET['ConfirmDelete']; ?> >
<input type="submit" name="Deleting" onclick=
      'this.form.ConfirmDelete.checked = deleteOrNot(this.form)' value="Delete this">

并像这样更改您的脚本

<script type='text/javascript'>
    function deleteOrNot(formEle)
    {
        if(typeof formEle == 'undefined')
        {
            formEle = forms[0];
        }
        var result = confirm('Are you sure you want to delete' + formEle.Delete_This.value + ' ? \nHey');
        if (result)
            alert(formEle.Delete_This.value + ' will be deleted');
        else
            alert('Not deleted');
        return result;
    }
</script>     
  

注意:我将表单作为参数添加到deleteOrNot函数中,并使用它来获取文本框的值。如果未选中,则复选框上的必填项将阻止表单提交

答案 2 :(得分:-1)

onclick="return confirm('Are you sure you want to delete?');"