AJAX Auto Refresh / Page without refresh after submitting Button - HTML / PHP












-1















My only problem is the page does not load auto refresh, tho it is loading when i refresh the page..



here is my html and ajax code with its database.




My Trigger Button




    <?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>';
}
?>



Fetching the Data in Database




Here is where I call the data in mysql from ajax to here.



$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';



My AJAX code
This is my AJAX code, here is where I got the problem the modal will not load right after I clicked the button, before it will work, I need to refresh the page how to fix this problem




 $('.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
});
}
});

});


I am new to ajax coding and syntax help me to finish this project please.










share|improve this question





























    -1















    My only problem is the page does not load auto refresh, tho it is loading when i refresh the page..



    here is my html and ajax code with its database.




    My Trigger Button




        <?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>';
    }
    ?>



    Fetching the Data in Database




    Here is where I call the data in mysql from ajax to here.



    $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';



    My AJAX code
    This is my AJAX code, here is where I got the problem the modal will not load right after I clicked the button, before it will work, I need to refresh the page how to fix this problem




     $('.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
    });
    }
    });

    });


    I am new to ajax coding and syntax help me to finish this project please.










    share|improve this question



























      -1












      -1








      -1








      My only problem is the page does not load auto refresh, tho it is loading when i refresh the page..



      here is my html and ajax code with its database.




      My Trigger Button




          <?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>';
      }
      ?>



      Fetching the Data in Database




      Here is where I call the data in mysql from ajax to here.



      $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';



      My AJAX code
      This is my AJAX code, here is where I got the problem the modal will not load right after I clicked the button, before it will work, I need to refresh the page how to fix this problem




       $('.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
      });
      }
      });

      });


      I am new to ajax coding and syntax help me to finish this project please.










      share|improve this question
















      My only problem is the page does not load auto refresh, tho it is loading when i refresh the page..



      here is my html and ajax code with its database.




      My Trigger Button




          <?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>';
      }
      ?>



      Fetching the Data in Database




      Here is where I call the data in mysql from ajax to here.



      $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';



      My AJAX code
      This is my AJAX code, here is where I got the problem the modal will not load right after I clicked the button, before it will work, I need to refresh the page how to fix this problem




       $('.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
      });
      }
      });

      });


      I am new to ajax coding and syntax help me to finish this project please.







      javascript php jquery html ajax






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 5:45









      Foo

      1




      1










      asked Nov 22 '18 at 5:42









      don fenderdon fender

      13




      13
























          1 Answer
          1






          active

          oldest

          votes


















          0














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


          just return false as firing an event when you submit a button inside a form.. :)






          share|improve this answer
























          • sir can you edit the code please im not really sure about it.. the code is just shared to me

            – don fender
            Nov 22 '18 at 5:51











          • which part should i put it

            – don fender
            Nov 22 '18 at 5:51











          • and then ill marked your answer as correct

            – don fender
            Nov 22 '18 at 5:51











          • Can u edit the code for the final answer pls

            – don fender
            Nov 22 '18 at 5:55











          • try it first.. just put "return false;" to the last "});" of the announcement click event.

            – user1308314
            Nov 22 '18 at 5:58











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424542%2fajax-auto-refresh-page-without-refresh-after-submitting-button-html-php%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














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


          just return false as firing an event when you submit a button inside a form.. :)






          share|improve this answer
























          • sir can you edit the code please im not really sure about it.. the code is just shared to me

            – don fender
            Nov 22 '18 at 5:51











          • which part should i put it

            – don fender
            Nov 22 '18 at 5:51











          • and then ill marked your answer as correct

            – don fender
            Nov 22 '18 at 5:51











          • Can u edit the code for the final answer pls

            – don fender
            Nov 22 '18 at 5:55











          • try it first.. just put "return false;" to the last "});" of the announcement click event.

            – user1308314
            Nov 22 '18 at 5:58
















          0














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


          just return false as firing an event when you submit a button inside a form.. :)






          share|improve this answer
























          • sir can you edit the code please im not really sure about it.. the code is just shared to me

            – don fender
            Nov 22 '18 at 5:51











          • which part should i put it

            – don fender
            Nov 22 '18 at 5:51











          • and then ill marked your answer as correct

            – don fender
            Nov 22 '18 at 5:51











          • Can u edit the code for the final answer pls

            – don fender
            Nov 22 '18 at 5:55











          • try it first.. just put "return false;" to the last "});" of the announcement click event.

            – user1308314
            Nov 22 '18 at 5:58














          0












          0








          0







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


          just return false as firing an event when you submit a button inside a form.. :)






          share|improve this answer













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


          just return false as firing an event when you submit a button inside a form.. :)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 5:48









          user1308314user1308314

          112




          112













          • sir can you edit the code please im not really sure about it.. the code is just shared to me

            – don fender
            Nov 22 '18 at 5:51











          • which part should i put it

            – don fender
            Nov 22 '18 at 5:51











          • and then ill marked your answer as correct

            – don fender
            Nov 22 '18 at 5:51











          • Can u edit the code for the final answer pls

            – don fender
            Nov 22 '18 at 5:55











          • try it first.. just put "return false;" to the last "});" of the announcement click event.

            – user1308314
            Nov 22 '18 at 5:58



















          • sir can you edit the code please im not really sure about it.. the code is just shared to me

            – don fender
            Nov 22 '18 at 5:51











          • which part should i put it

            – don fender
            Nov 22 '18 at 5:51











          • and then ill marked your answer as correct

            – don fender
            Nov 22 '18 at 5:51











          • Can u edit the code for the final answer pls

            – don fender
            Nov 22 '18 at 5:55











          • try it first.. just put "return false;" to the last "});" of the announcement click event.

            – user1308314
            Nov 22 '18 at 5:58

















          sir can you edit the code please im not really sure about it.. the code is just shared to me

          – don fender
          Nov 22 '18 at 5:51





          sir can you edit the code please im not really sure about it.. the code is just shared to me

          – don fender
          Nov 22 '18 at 5:51













          which part should i put it

          – don fender
          Nov 22 '18 at 5:51





          which part should i put it

          – don fender
          Nov 22 '18 at 5:51













          and then ill marked your answer as correct

          – don fender
          Nov 22 '18 at 5:51





          and then ill marked your answer as correct

          – don fender
          Nov 22 '18 at 5:51













          Can u edit the code for the final answer pls

          – don fender
          Nov 22 '18 at 5:55





          Can u edit the code for the final answer pls

          – don fender
          Nov 22 '18 at 5:55













          try it first.. just put "return false;" to the last "});" of the announcement click event.

          – user1308314
          Nov 22 '18 at 5:58





          try it first.. just put "return false;" to the last "});" of the announcement click event.

          – user1308314
          Nov 22 '18 at 5:58


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424542%2fajax-auto-refresh-page-without-refresh-after-submitting-button-html-php%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Wiesbaden

          To store a contact into the json file from server.js file using a class in NodeJS

          Marschland