【问题标题】:JavaScript Confirm Box in PHP tag Keeps returning TRUEPHP标签中的JavaScript确认框不断返回TRUE
【发布时间】:2016-01-20 13:32:06
【问题描述】:

我正在尝试在 php 标签中添加 javascript 代码以在按下按钮时进行处理,它应该确认用户是否真的想要删除图像。

我的问题是,一旦显示确认框 - 它对“取消”和“确定”都返回 true - 它以任何一种方式执行代码以从数据库中删除图像。

有人可以提供任何建议吗?

if (isset($_POST['remove'])) {
    $imgId = $_POST['imgId'];

    echo '<script type="text/javascript">
        var h = "Are you sure you to delete Image ID: '.$imgId.'";

        if (!confirm(h)) {
            event.preventDefault();
        } else {' .
            mysql_query("DELETE FROM images WHERE img_id='$imgId';") . '
        }

    </script>';
}

【问题讨论】:

  • 您不能将 php 函数调用放在 javascript 中。您必须通过向服务器发送 http 请求(可能是 ajax)来触发 DELETE 查询。
  • 它对我来说很好用。我得到真假。
  • @bub - 这是第一次确认 - 用作测试 - 第二次确认总是删除图像并执行查询
  • 我确实提到了 - 我说它执行查询
  • 你需要这样的东西:?&gt;&lt;script type="text/javascript"&gt; var img = "&lt;?PHP echo $imgId; ?&gt;"; var h = "Are you sure you to delete Image ID: "+img; if (confirm(h)) { $.post("delete.php",{"imgId":img}); } &lt;/script&gt; - 这似乎很愚蠢,因为无论如何你似乎都从提交中返回

标签: javascript php jquery mysql confirm


【解决方案1】:

正如有人建议的那样,在这种情况下,最好的方法是使用 JS(在这种情况下我将使用 jQuery)并在其自己的 php 文件中进行 php 调用。这里有一个简短的 html sn-p:

<a href="#" class="js-removeImg" data-image="10">Remove</a>

数据图像值 (10) 将从 php 获取,因此您必须准确知道要删除的图像。我通常喜欢为将在任何 javascript 触发器 js-removeImg 中链接的类添加一个“js-”前缀,以分隔样式和 javascript。

$(document).ready(function(){


  $('.js-removeImg').on('click', function(e){
      e.preventDefault(); // Disables default click's behaviour

      var $this = $(this);  // Cache current jQuery element

      var imgID = $this.data('image');
      // ^ Stores the image id we want to handle
      // The value comes from PHP

      var deleteUrl = "deleteImg.php";
      // ^ A php file where the "delete" query will be available
      // As parameter it should accept the image id: $_REQUEST['imageID']
      // imageID should be exactly the same with the one inside date object from below

      var string = "Are you sure you want to delete the image with the id __IMGID__ ?";
      // ^Stores the string we want to alert in a nicer way, easy to edit in future      

      var confirm = window.confirm( string.replace('__IMGID__', imgID) );

      // User clicked "ok"
      if (confirm) {
        // So we are going to tell this to the server (php file)
        $.ajax({
          method: 'POST',
          url : deleteUrl,
          data : {
            imageID: imgID
          },
          success : function(data) {
            console.log(data);
            // Handle the data according to the answer received from PHP file
            // E.g : shown an alert which say that is done or not etc
          }
        });
      } else {
        // User clicked false
      }

  })
});

以上是帮助您完成任务的 jQuery 代码。我认为 cmets 已经足够清楚了。

否则,如果出现问题,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多