【问题标题】:Bootstrap dismissable success message not showing longer with ajaxBootstrap 可关闭的成功消息不再使用 ajax 显示
【发布时间】:2023-03-25 18:54:01
【问题描述】:

我有一个使用 PHP post 方法获取数据的 ajax 表单。我没有使用 Javascript 的警报功能,而是调用了引导成功消息。但是问题它并没有显示更长时间仅持续不到一秒钟。如何在用户手动关闭它之前显示它的长度,因为它是可关闭的。

编码部分:

function send_form() {
  $.ajax({
     url: "./admin-security.php",
     type: "POST",
     data: {
       ip: $("#ip").val()
     },
     success: function(response) {
          if(response==1) {
               $("#ajax-ip-success").show();
              location.href = "./admin-security.php";
          }
          else alert("Fail! DataBase Error");
      }
  });
 }
<?php
//--AJAX PART CALLING
      if(isset($_POST['ip'])){
          if($IP = filter_input(INPUT_POST, 'ip', 
            FILTER_SANITIZE_STRING)){
          $add_ip = $mysqli->prepare("INSERT INTO block_ip(b_ip) 
            VALUES(?)");
          $add_ip->bind_param("s",$IP);
          $add_ip->execute();
          $add_ip->store_result();
          $add_ip->close();
         echo 1;
          }
          else {
                echo 0;
          }
       exit;
      }
?>
<div class="alert alert-success alert-dismissable fade in" style="display: none;" id="ajax-ip-success">
                  <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                  <strong>Success!</strong> IP added successfully.
                  </div>
<div class="form-horizontal">
        <div class="form-group">
       <label class="control-label col-sm-2" for="ip"> Enter IP:</label>
        <div class="col-sm-8">
            <input type="text" name="ip" class="form-control" id="ip" />
      </div></div>
     <div class="form-group"> 
        <div class="col-sm-offset-2 col-sm-8">
       <button type="button" onClick="send_form()" class="btn btn-default" 
        >Submit</button>
           </div></div>
         </div>

【问题讨论】:

  • 这是正确的行为吗?这意味着你的服务器端响应很快,有什么问题吗?如果你想延迟它,你可以使用setTimeout(),或者成功不调用重定向,而是基于变量,当用户点击关闭时,只重定向它
  • 服务器端没有问题。它只是警报消息显示得那么快,用户无法阅读所写的内容。
  • 我用 php 部分更新了我的问题
  • 我试过setTimeout($("#ajax-ip-success").show(),7000);没用。
  • setTimeout(function(){ location.href = "./admin-security.php"; },2000);

标签: javascript php jquery ajax twitter-bootstrap


【解决方案1】:

您可以为位置重定向设置超时

setTimeout(function(){
 location.href = "./admin-security.php"; 
},2000); 

【讨论】:

  • 这里可以添加点击功能吗?这样如果用户关闭了可忽略的警报,它就会重定向?
  • ya.set timeout 只等待被称为 2000 的特定时间
猜你喜欢
  • 2016-12-27
  • 1970-01-01
  • 1970-01-01
  • 2017-10-25
  • 1970-01-01
  • 2023-04-06
  • 2021-10-28
  • 1970-01-01
  • 2021-04-06
相关资源
最近更新 更多