简体   繁体   English

在执行PHP之前如何弹出JavaScript确认窗口?

[英]How to popup a JavaScript confirmation window before executing PHP?

How to popup a JavaScript confirmation window before executing: 执行前如何弹出JavaScript确认窗口:

<form action="index.php" method="get">
  <input type="hidden" name="act" value="run">
  <input type="submit" value="Clear map">
</form>
<?php
  if (!empty($_GET['act'])) {
        mysql_query("TRUNCATE TABLE mapinfo");
        $page = $_SERVER['PHP_SELF'];
        header("Location: $page");
  } 
?>

User has to press OK to execute PHP or Cancel to not. 用户必须按“确定”才能执行PHP,否则请按“取消”。

<?php
  // mysql connection
  if ($_GET['act'] == 'run') {
        mysql_query("TRUNCATE TABLE mapinfo");
        $page = $_SERVER['PHP_SELF'];
        header("Location: $page"); // for this to work, script must not output something before, otherwise use echo '<script type="text/javascript">window.location = \''.$page.'\';</script>';
        die;
  } 
?>
<form action="index.php" method="get" onsubmit="return confirm('Are you sure you want to truncate `mapinfo`?');">
  <input type="hidden" name="act" value="run">
  <input type="submit" value="Clear map">
</form>

Use javascript confirm() to do so. 使用javascript confirm()这样做。 Add an onclick attribute to the submit button that will call a confirm dialog and check with javascript if option yes then some code and if not then some other code. 向提交按钮添加一个onclick属性,该属性将调用确认对话框,并使用javascript检查是否为yes选项,然后是一些代码,如果不是,则是其他代码。

PHP script is evaluated on the server, before the site is sent to the client. 在将站点发送到客户端之前,将在服务器上评估PHP脚本。 The javascript is evaluated only when the client starts processing the page. 仅当客户端开始处理页面时才评估javascript。 So you cant really do anything in javascript before the php script is evaluated. 因此,在评估PHP脚本之前,您实际上无法在javascript中做任何事情。

A solution would be to set up a proxy page that handle the javascript stuff you need, and then refer you to the php page. 一种解决方案是设置一个代理页面来处理所需的javascript内容,然后将您引向php页面。

Hope that helps! 希望有帮助!

试试这一行,

<a href="truncate_page.php" onclick="return confirm('Are you sure you want to truncate?')">Truncate</a> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM