【问题标题】:How to display confirm box on website in php如何在php中的网站上显示确认框
【发布时间】:2016-10-10 11:53:13
【问题描述】:

我想显示一个带有两个按钮是和否的确认框,当用户按下是时,将显示索引页面,当用户按下否时,此重定向到另一个页面。这确认 msgbox 仅在第一次打开网站时显示一次,而不是在每次重新加载页面时显示。谢谢

【问题讨论】:

标签: javascript php session alert confirm


【解决方案1】:

你可以试试这个代码

 <script>
 var confirm = confirm("sample message?");
 if(confirm) {
     // index page
     window.location.url = ""; // your url index page
  } else {
     // other page
     window.location.url = ""; // your url other page
  }
 </script>

【讨论】:

  • 我已经尝试过了,但是如果我在 onLoad 事件上执行它,那么它会在每次重新加载页面时执行,并且我无法通过 javascript 设置 php 会话变量(所以我无法在下一个测试重新加载页面)
  • 是的,它会一次又一次地显示,因为你把它放在 onload 事件上
  • 那么我该如何解决它。
【解决方案2】:

您必须在 javascript 中完成所有工作。 您可以使用的 Javascript 函数。 另外,您需要在浏览器中设置 cookie 并检查是否设置了 cookie。

function myFunction() {
    var x;
    if (confirm("Press a button!") == true) {
        x = "You pressed OK!";
    } else {
        x = "You pressed Cancel!";
    }
    document.getElementById("demo").innerHTML = x;
}

希望这会有所帮助。

【讨论】:

    【解决方案3】:

    function myFunction() {
        var txt;
        var r = confirm("Press a button!");
        if (r == true) {
            txt = "You pressed OK!";
        } else {
            txt = "You pressed Cancel!";
        }
        document.getElementById("demo").innerHTML = txt;
    }
    <button onclick="myFunction()">Try it</button>
    
    <p id="demo"></p>

    【讨论】:

      【解决方案4】:

      <html>
      <head>
      	<title>Are you sure?</title>
      	
      	<script>
      	    function areYouSure() {
                  var res = confirm("Are you sure?");
                  return res; //true or false
              }
      	</script>
      	
      </head>
      <body>
                          
      <form method="POST" action="action_page.php">
      <!--
      ...
      -->
      <input type="submit" onclick="return areYouSure()"/>
      
      </form>
      
      </body>
      </html>

      【讨论】:

      • 嗨 AA,很高兴您花时间回答,但真正有帮助的是一些描述您为什么推荐这个以及它的作用,记住所有技能水平和经验的人可能会在以后在这里寻找答案:) stackoverflow.com/help/answering
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多