Implementation code for partial refresh of HTML page

Implementation code for partial refresh of HTML page

Event response refresh: refresh only when requested

1. Get HTML elements through JS HTML DOM or jQuery, listen to page events through DOM methods or jQuery methods, and get user requests;

2. Submit the user request to the server through Ajax. The server returns the result after processing, and then Ajax receives the data.

3. Load data into the page through DOM method or jQuery method, and the event response refresh is completed.

$('#input_date').keypress(function(e){

  if(e.keyCode=='13'){

    $.ajax({

      type: "POST",

      url: "inquire_date.php",

      data: {

        birth:null,

//1. Get user requests (ie certain events) and send them to the server for processing date:$('#input_date input').val()

      },

      dataType: "json",

//2. Get data from the server success: function(data){

        if (data.success) {

          var festival = data.fetivalInquireResult;

//3. Load the acquired data into the page to implement page event response refresh$('#show_festival').text(festival);

        } else {

          $('#show_festival').text("Failed to get festival");

        }  

      },

      error: function(jqXHR){     

        alert("Error: " + jqXHR.status);  
      },     

    });

  $('#festival').hide();

  $('#response_festival').show();

  }

});

Partial automatic refresh: The partial page will be automatically refreshed even if no request is made

1. Use a timer function such as setTimeout() to let Ajax get data from the server at regular intervals;

2. Load data into the page through DOM method or jQuery method to achieve automatic refresh of part of the page.

$(document).ready(function(e){

    setTimeout('updateShow()',0);

});

/*Partial automatic refresh page data*/

function updateShow(){

  $.ajax({

    type: "GET",

    url: "inquire_date.php?data=" + "inquire",

    dataType: "json",

//1. Get data from the server regularly through a timer success: function(data) {

      if (data.success) {

        var agesFormat = data.agesFormat;

        var daysFormat = data.daysFormat;

//2. Load data into the page to achieve automatic refresh$('#ages').text(agesFormat);

       $('#days').text(daysFormat);

      } else{

        alert("Failed to obtain data");

      }

    },

    error: function(jqXHR){     

      alert("Error: " + jqXHR.status);  

    },

  });

  setTimeout('updateShow()',500);

}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  How to insert Emoji expressions into MySQL

>>:  Docker image creation, uploading, pulling and deployment operations (using Alibaba Cloud)

Recommend

HTML Frameset Example Code

This article introduces a framework made by Frame...

Docker-compose quickly builds steps for Docker private warehouse

Create docker-compose.yml and fill in the followi...

Tips for data statistics in MySQL

As a commonly used database, MySQL requires a lot...

How to install Mysql5.7 in Centos6

environment Centos 6.6 MySQL 5.7 Install If the s...

Javascript basics about built-in objects

Table of contents 1. Introduction to built-in obj...

Semantics, writing, and best practices of link A

The semantics, writing style, and best practices ...

td width problem when td cells are merged

In the following example, when the width of the td...

mysql: [ERROR] unknown option '--skip-grant-tables'

MySQL database reports ERROR 1045 (28000): Access...

How to build Git service based on http protocol on VMware+centOS 8

Table of contents 1. Cause 2. Equipment Informati...

VMware configuration hadoop to achieve pseudo-distributed graphic tutorial

1. Experimental Environment serial number project...

How to implement Hover drop-down menu with CSS

As usual, today I will talk about a very practica...

How to understand Vue's simple state management store mode

Table of contents Overview 1. Define store.js 2. ...