javascript - 成功发布新闻后显示 Bootstrap 警告框

标签 javascript php jquery html twitter-bootstrap

这就是我想要做的。每当管理员成功发布新闻时,我希望 div 成功警报弹出并在 3 秒后消失。我想把它放在文本“添加新闻”之后,如图所示。有人可以告诉我如何执行此操作吗?

这是我的作品的示例图片。 enter image description here

这是我添加新闻的 php 代码。

  <?php
    include_once('connection.php');
    session_start();
   $username = ucfirst($_SESSION['username']);

  if(isset($_POST['submit'])){

    $title = $_POST['title'];
    $date = $_POST['date'];
    $content = $_POST['content'];
    $file=$_FILES['image']['tmp_name'];
    $image= @addslashes(file_get_contents($_FILES['image']['tmp_name']));
    $image_name= addslashes($_FILES['image']['name']);
    move_uploaded_file($_FILES["image"]["tmp_name"],"img/" . $_FILES["image"]["name"]);
    $newsimage="img/" . $_FILES["image"]["name"];



    $sql ="INSERT into news (news_title, news_date, news_content, news_image) VALUES ('$title', '$date', '$content', '$newsimage')";
    mysqli_query($con, $sql);



  }
  ?>

这是我的表单,其中有警告框。

 <div id="page-wrapper">

            <div class="container-fluid">

                  <a class='btn btn-info' href ="news.php" style="margin-bottom:25px; float:right;" ><span class="glyphicon glyphicon-search"></span> View all news</a>


            <form method="post" action ="addnews.php" enctype="multipart/form-data">

             <h4>Add News</h4>

             <div class="alert alert-success" id="success-alert">
              <strong>Successfully posted!</strong>
             </div>

                <div class="form-group">
                       <label for="title">News Title</label>
                        <input type="text" name="title" class="form-control title" id="title" placeholder="News Title" required >
                </div>  

              <div class="form-group">
               <label for="title">Date</label>
                   <input type="text" name="date" class="form-control date" id="date" placeholder="Date" required >  
                </div>

                <div class="form-group">
                      <label for="content">News Content</label>
                     <textarea class="form-control content" name="content" rows="5" id="content" required></textarea>
                </div>    


                      <img id="blah" src="" alt="image here" width="200px" height="140px"/>
                       <input id="image" name="image" class="fileupload" type="file" accept="image/*"/>
                     <button type="submit" name="submit" id="postnews" class='btn btn-info '>Post news</button>
              </form>




        </div>
        <!-- /#page-wrapper -->

    </div>
    <!-- /#wrapper -->

最佳答案

您可以通过 Php session 在表单提交时显示警告框,例如:

<?php
if(isset($_SESSION['form_submit'])) {
 echo '<div class="alert alert-success alert-dismissable" id="success-alert">';
          echo '<strong>Successfully posted!</strong>';
         echo '</div>';
 unset($_SESSION['form_submit']);
}
?>

您已在表单提交时设置此 session 变量:- $_SESSION['form_submit']=true

要自动隐藏此警告框,您可以使用此 javascript 代码

$(".alert-dismissable").fadeTo(2000, 500).slideUp(500, function(){
 $(".alert-dismissable").alert('close');
});

关于javascript - 成功发布新闻后显示 Bootstrap 警告框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38341155/

相关文章:

javascript - 用于切换 sql 结果的 Jquery 按钮

javascript - 确定 var 是否是 javascript 中的 elementFinder 对象的方法是什么?

javascript - 将 React 函数转换为 React 类

javascript - 删除 ACE 编辑器中的自动换行偏移

php - 为什么我们使用 try block 来抛出异常。我们不能在没有 try block 的情况下简单地抛出并捕获它们吗?它的重要性是什么?

php - register_shutdown_function() 和 set_error_handler() 可以捕获相同的错误吗?

javascript - 这段代码是否将类属性添加到 foo 数组中?

JSON 和 jQuery.ajax

php - 在 PHP 中进行系统调用时如何指定环境变量?

javascript - 如何使用 jQuery 防止 mouseenter 事件操作