尝试将 javascript 确认框与 php post 方法结合起来?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7081016/
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-10-25 22:53:52  来源:igfitidea点击:

Try to combine javascript confirm box with php post method?

javascriptconfirmation

提问by user878729

Here is the Javascript code, just for test:

这是Javascript代码,仅供测试:

<script type="text/javascript">
    function show_confirm()
        {
            var r=confirm("Are you sure?");
            if (r==true)
            {                   
            }
            else
            {                    
            }
        } 
</script>

And this is the php code:

这是php代码:

<form action="Test.php" method="post">
    <input type="text" name="test"/>
    <input type="submit" value="submit" onclick="show_confirm()"/>
</form>

So if the submit button is clicked, a confirm box will pop out. And if "OK" button is clicked, the page will be redirected to Test.php and post method will be executed, like a normal php page submit operation. Or else, if the "cancel" button is clicked, i want it stay the same page and post method won't be executed. Is this function can be implemented and how to modify the code? I know PHP is for server and JS is for client. I'm not sure if I can mix them like this. Or I should use Ajax? Thanks for help!

因此,如果单击提交按钮,则会弹出一个确认框。如果单击“确定”按钮,页面将被重定向到 Test.php 并执行 post 方法,就像普通的 php 页面提交操作一样。否则,如果单击“取消”按钮,我希望它保持在同一页面并且不会执行 post 方法。请问这个功能是否可以实现以及如何修改代码?我知道 PHP 用于服务器,而 JS 用于客户端。我不确定我是否可以像这样混合它们。还是我应该使用 Ajax?感谢帮助!

回答by rlemon

You can do this directly from the button. return falsewill cancel the submission ... so....

您可以直接从按钮执行此操作。return false将取消提交...所以....

<input type="submit" onclick="javascript:return confirm('Are you sure?');">

if the user clicks 'ok' the dialog will return true, otherwise false.

如果用户单击“确定”,对话框将返回真,否则为假。

to be complete here it is in function form.

这里是完整的,它是函数形式。

function show_confirm()
{
  var r = confirm("Are you sure?");
  if(r == true)
  {
     // do something
     return true;
  } else {
     // do something
     return false;
  }
}

edit: reading this almost 4 years later I'm compelled to change it.

编辑:将近 4 年后读到这篇文章,我不得不改变它。

function show_confirm()
{
  var r = confirm("Are you sure?");
  if(r)
  {
     // do something for true
  } else {
     // do something for false
  }
  return r;
}

call it with <input type="submit" onclick="javascript:return show_confirm();">

<input type="submit" onclick="javascript:return show_confirm();">

I believe this will also handle the case where nothing is selected but the dialog is closed.

我相信这也将处理未选择任何内容但关闭对话框的情况。

note: inline event listeners should be avoided. attach an event listener with Javascript.

注意:应避免使用内联事件侦听器。使用 Javascript 附加事件侦听器。

回答by Cam

The best way to do this is as follows:

最好的方法如下:

<script>
    function show_confirm(){
        return confirm("Are you sure?");
    }
</script>
<input type="submit" onclick="return show_confirm();">

The reason I use show_confirm instead of the built-in function directly is because if you want to change the functionality of the confirm popup, it is much harder if you are using the builtin function everywhere in your code.

我直接使用 show_confirm 而不是内置函数的原因是,如果您想更改确认弹出窗口的功能,如果您在代码中的任何地方都使用内置函数,那就更难了。

Abstracting out this kind of functionality and writing clear, simple code like this will make you a better programmer :)

抽象出这种功能并编写像这样清晰、简单的代码将使您成为更好的程序员:)



warning: as indicated in my comments below, this will only work if the user explicitly clicks the submit button. If the user submits the form by some other method, the confirm box will not show. See http://www.w3schools.com/jsref/event_form_onsubmit.aspif you want to show a confirm regardless of how the form was submitted.

警告:正如我在下面的评论中所指出的,这仅在用户明确单击提交按钮时才有效。如果用户通过其他方式提交表单,则不会显示确认框。如果您想显示确认而不管表单是如何提交的,请参阅http://www.w3schools.com/jsref/event_form_onsubmit.asp

回答by genesis

<script type="text/javascript">
    function show_confirm()
        {
            var r=confirm("Are you sure?");
            if (r==true)
            {     
                 document.location.href= 'page.php';              
            }
            else
            {                    
                 return false;
            }
        } 
</script>

回答by Liam Newmarch

Simplifying rlemon's answer even further (no need for the javascript:return, also fixed the inner double quotes):

进一步简化 rlemon 的答案(不需要javascript:return,也修复了内部双引号):

<input type="submit" onclick="return confirm('Are you sure?')" />