提交按钮后AJAX自动刷新/页面无需刷新-HTML / PHP

时间:2018-11-22 05:42:30

标签: javascript php jquery html ajax

我唯一的问题是页面无法加载自动刷新,因此当我刷新页面时它正在加载。

这是我的html和ajax代码及其数据库。

  

我的触发按钮

    <?php
        $data = mysqli_query($con,"SELECT * FROM announcement");
        $count = mysqli_num_rows($data);
        if ($count != 0) {
            while($row = mysqli_fetch_array($data)) { 
                echo '
                    <button class="btn btn-danger nav-link delannouncement" id="delannouncement">Resume System</button>';
                $dataid = $row['id'];
            }
        }else{
            echo '<button class="btn btn-primary nav-link announcement" id="announcement">Passenger Announcement</button>';
        }
    ?>
  

数据库

中获取数据

这是我在mysql中将数据从ajax调用到这里的地方。

$data = mysqli_query($con,"SELECT * FROM announcement");
    $count = mysqli_num_rows($data);
    if ($count != 0) {
      while($row = mysqli_fetch_array($data)) { 
          echo '<div id="myModal" class="modal" style="position:fixed;  display: none; padding-top: 100px; 
          left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgb(0,0,0); background-color: rgba(0,0,0,0.9); ">
                <div class="container" style="width: 100%">
                <div class="card col-12" style="background-color: red; color: white;">
                <div class="card-header">
                    <p><h3 class="text-center">Announcement</h3></p>
                </div>
                  <div class="card-body text-center">
                      <h5>'.$row['additional_info'].'</h5>
                      <p>Please click refresh button to resume after the announcement</p>
                  </div>
                  </div>

                </div>
                    <img class="modal-content" id="img01">
                    </div>';
                  $dataid = $row['id'];
          }
      }

        ?>
        </div>

        <script>
        document.getElementById('myModal').style.display='block';
  

我的 AJAX代码   这是我的AJAX代码,这是我单击按钮后模态无法立即加载的问题,在它起作用之前,我需要刷新页面以解决此问题

 $('.announcement').on("click", function () {
        $.confirm({
            title: 'Announcement',
            content: '' +
                '<form action="" class="formName">' +
                '<div class="form-group">' +
                '<label>Announcement Info</label>' +
                '<input type="text" placeholder="Enter announcement here" class="add-info form-control" required />' +
                '</div>' +
                '</form>',
            buttons: {
                formSubmit: {
                    text: 'Submit',
                    btnClass: 'btn-blue',
                    action: function () {
                        var addInfo = this.$content.find('.add-info').val();
                            $.confirm({
                                type: 'red',
                                theme: 'material',
                                title: 'Are you sure you want to make an announcement?',
                                content:'<strong>Announcement Info:</strong> ' + addInfo,
                                buttons: {
                                    Yes: {
                                        btnClass: 'btn-green',
                                        action: function () {
                                            $.ajax({
                                                type: "POST",
                                                url: "announcement.php",
                                                data: {
                                                    addInfo: addInfo
                                                },
                                                dataType: "text",
                                                success: function (data) {
                                                    window.location.replace("change-password.php");

                                                },
                                                error: function (err) {
                                                    console.log(err);

                                                }

                                            });

                                        }

                                    },
                                    No: {
                                        text: 'No', // With spaces and symbols
                                        action: function () {
                                            $.alert('Announcement Cancelled');
                                        }
                                    }
                                }
                            });
                    }
                },
                cancel: function () {
                    //close
                },
            },
            onContentReady: function () {
                // bind to events
                var jc = this;
                this.$content.find('form').on('submit', function (e) {
                    // if the user submits the form by pressing enter in the field.
                    e.preventDefault();
                    jc.$$formSubmit.trigger('click'); // reference the button and click it
                });
            }
        });

    });

我是ajax编码的新手,语法可以帮助我完成这个项目。

1 个答案:

答案 0 :(得分:0)

$('.announcement').on("click", function () {
        $.confirm({
            ///YOUR LONG CODE
         });
    //ALL YOU NEED
    return false;
});

在表单内提交按钮时,触发事件时返回false即可。:)