Javascript 之后用 PHP 函数确认

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8680491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:02:58  来源:igfitidea点击:

Javascript confirm with PHP function afterward

phpjavascriptconfirm

提问by Catia

I would like a way to have a confirm box pop up when a user clicks "delete" to remove a record.

我想要一种在用户单击“删除”以删除记录时弹出确认框的方法。

Something like:

就像是:

<script language="JavaScript"  type="text/javascript" >

function confirmDelete() {
    if(confirm("Would you like to delete the selected products?")) {
        <?php 
            $allCheckBoxId = $_POST['checkbox'];
            array_map ('mysql_real_escape_string', $allCheckBoxId);
            $ids = implode(",", $allCheckBoxId);
            $sql = "DELETE FROM products WHERE `id` IN ($ids)";
            mysql_query($sql);
        ?>
    }
}

</script>

With an onclick:

通过点击:

echo "<input type='submit' name='submit' value='Delete'  onclick='confirmDelete()' />";

But I know it's not possible to be using Javascript & PHP together like this. Is there another way to do this? Maybe PHP has its own kind of confirm? Please give any ideas!

但我知道不可能像这样一起使用 Javascript 和 PHP。有没有另一种方法可以做到这一点?也许PHP有自己的确认方式?请给出任何想法!

Thank you.

谢谢你。

回答by Jules

It is pretty easy to make use of AJAX for this case. Simply place your PHP in the scriptDelete.phpfile and make sure you pass the proper arguments and their values in the dataproperty. For this example I just pass an idof 5.

在这种情况下使用 AJAX 非常容易。只需将您的 PHP 放在scriptDelete.php文件中,并确保在data属性中传递正确的参数及其值。在这个例子中,我传递一个ID5

<script type="text/javascript">

    function confirmDelete() {
        if (confirm('Are you sure you want to delete this?')) {
            //Make ajax call
            $.ajax({
                url: "scriptDelete.php",
                type: "POST",
                data: {id : 5},
                dataType: "html", 
                success: function() {
                    alert("It was succesfully deleted!");
                }
            });

        }
    }
</script>

<input type='submit' name='submit' value='Delete'  onclick='return confirmDelete()' />

Another way would be to post the form back to its own page like:

另一种方法是将表单发布回其自己的页面,例如:

<form method="post" action="yourpage.php">
    <input type="submit" name="submit" value="delete" onclick="return confirm('Are you sure you want to delete this?')" />
</form>

So you simply post the form back to yourpage.phpand on top of this page you do something like:

因此,您只需将表单发回yourpage.php并在此页面的顶部执行以下操作:

<?php

   //Means the form was submitted
   if ($_POST['submit']) {
      $id = $_POST['idToDelete'];
      $query = "DELETE FROM products WHERE id = " . mysql_real_escape_string($id);
      mysql_query($query) or die(mysql_error());
   }
?>

回答by Mitch Grande

You should move the event to the form, and use onsubmit="confirmDelete()". If you return false within the ConfirmDelete function, the form submit is cancelled. If you return true, the form will get submitted like normal, and you can process it like a regular form on the server side.

您应该将事件移动到表单中,并使用 onsubmit="confirmDelete()"。如果您在 ConfirmDelete 函数中返回 false,则表单提交将被取消。如果您返回 true,表单将像正常一样提交,您可以像服务器端的常规表单一样处理它。

<script type="text/javascript">
function confirmDelete() {
    return window.confirm("Are you sure you want to delete this record?");
}
</script>

<form action="myDelete.php" onsubmit="confirmDelete()">
    ....inputs
    <input type="submit" name="submit" value="Delete">
</form>

Then have regular processing code on the server side

然后在服务器端有规律的处理代码

回答by Faisal

Easy way to confirm, instead of creating whole new function. Do this

确认的简单方法,而不是创建全新的功能。做这个

<input type="submit" name="Delete" onClick='return confirm("Sure you want Delete this Page?")' />

this will exactly respond the same way.

这将完全以相同的方式响应。

回答by Xuntar

The easiest way is to do a javascript function before you submit. If you click on false, the return will be false and it will not be submitted:

最简单的方法是在提交之前执行 javascript 函数。如果点击false,则返回false,不会提交:

<form action="something.php" onSubmit="return confirm('Are you sure?');" method="post">
// Fields
// Fields
<input type="submit" name="Submit" class="button" value="Submit">
</form>

回答by Brendon Dugan

Ajax. Place your php in a separate file, and use ajax to send it the id you want to use in your query. I recommend jQuery .post

阿贾克斯。将您的 php 放在一个单独的文件中,并使用 ajax 将您要在查询中使用的 id 发送给它。我推荐jQuery .post