Hướng dẫn what is advanced ajax in php? - ajax nâng cao trong php là gì?

Ajax với JSON

Trong phòng thí nghiệm trước, bạn đã làm việc trên Ajax với văn bản thuần túy. Như một vấn đề thực tế, bạn có thể xử lý dữ liệu AJAX với các định dạng khác nhau như văn bản thuần túy, XML và JSON. Vì nhiều ứng dụng sử dụng JSON làm cấu trúc truyền dữ liệu, phòng thí nghiệm này giới thiệu những điều cơ bản về JSON, tạo dữ liệu JSON trong PHP và phân tích dữ liệu JSON trong PHP.

JSON là gì?

JSON là viết tắt của ký hiệu đối tượng JavaScript. Nó cung cấp một cách có cấu trúc để lưu trữ và trao đổi dữ liệu. JSON hỗ trợ nhiều loại dữ liệu khác nhau như chuỗi, số, boolean, mảng, loại null và đối tượng.

Hướng dẫn JSON: http://www.w3schools.com/js/js_json_js_intro.asp

Cú pháp JSON: http://www.w3schools.com/js/js_json_syntax.asp

Sản xuất JSON trong PHP

Mã hóa JSON trong PHP5.5: http://php.net/manual/en/function.json-encode.php

Phân tích cú pháp JSON trong PHP

Giải mã JSON trong PHP5.5: http://php.net/manual/en/function.json-decode.php

Chi tiết về ví dụ

Phần này giải thích các bước chính của ví dụ.

Hỗ trợ MySQL

Hỗ trợ hệ thống cơ sở dữ liệu MySQL có được một số lượng dữ liệu tại một thời điểm bằng cách sử dụng từ khóa

$sql2 = "select student_ID, last_Name, course_Name, marks from students 
where trim(last_Name) is not null and trim(last_Name)!='' limit " . $startRecord . ",5";

$result = mysqli_query($conn, $sql2);

$sResp[] =  array();

while ($row = mysqli_fetch_assoc($result))
{
    $sRow["stid"]=$row["student_ID"];
    $sRow["lname"]=$row["last_Name"];
    $sRow["course"]=$row["course_Name"];
    $sRow["marks"]=$row['marks'];
    $sResp[] = $sRow;
}
echo json_encode($sResp);
3. Ví dụ,

mysql> select student_ID, last_Name, course_Name, marks from students 
where trim(last_Name) is not null and trim(last_Name)!='' limit 1,5;

+------------+-----------+-------------+-------+
| student_ID | last_Name | course_Name | marks |
+------------+-----------+-------------+-------+
| 200266200  | Abbad     | CS110       |    85 |
| 200266200  | Abbad     | CS210       |    90 |
| 200266202  | Rai       | CS100       |  72.5 |
| 200266202  | Rai       | CS110       |  59.2 |
| 200266202  | Rai       | CS215       |  82.6 |
+------------+-----------+-------------+-------+
5 rows in set (0.00 sec)

Trong ví dụ,

$sql2 = "select student_ID, last_Name, course_Name, marks from students 
where trim(last_Name) is not null and trim(last_Name)!='' limit " . $startRecord . ",5";

$result = mysqli_query($conn, $sql2);

$sResp[] =  array();

while ($row = mysqli_fetch_assoc($result))
{
    $sRow["stid"]=$row["student_ID"];
    $sRow["lname"]=$row["last_Name"];
    $sRow["course"]=$row["course_Name"];
    $sRow["marks"]=$row['marks'];
    $sResp[] = $sRow;
}
echo json_encode($sResp);
4 là phần bù từ hàng ban đầu bắt đầu từ 0. Số
$sql2 = "select student_ID, last_Name, course_Name, marks from students 
where trim(last_Name) is not null and trim(last_Name)!='' limit " . $startRecord . ",5";

$result = mysqli_query($conn, $sql2);

$sResp[] =  array();

while ($row = mysqli_fetch_assoc($result))
{
    $sRow["stid"]=$row["student_ID"];
    $sRow["lname"]=$row["last_Name"];
    $sRow["course"]=$row["course_Name"];
    $sRow["marks"]=$row['marks'];
    $sResp[] = $sRow;
}
echo json_encode($sResp);
5 cho phép năm hàng tối đa trở lại. Đó là,
$sql2 = "select student_ID, last_Name, course_Name, marks from students 
where trim(last_Name) is not null and trim(last_Name)!='' limit " . $startRecord . ",5";

$result = mysqli_query($conn, $sql2);

$sResp[] =  array();

while ($row = mysqli_fetch_assoc($result))
{
    $sRow["stid"]=$row["student_ID"];
    $sRow["lname"]=$row["last_Name"];
    $sRow["course"]=$row["course_Name"];
    $sRow["marks"]=$row['marks'];
    $sResp[] = $sRow;
}
echo json_encode($sResp);
6 thực sự trả lại các bản ghi từ hàng thứ 2 đến hàng thứ 6.

Trong PHP: Mã hóa dữ liệu thành JSON với hàm PHP json_encode ()

Sử dụng chức năng PHP

$sql2 = "select student_ID, last_Name, course_Name, marks from students 
where trim(last_Name) is not null and trim(last_Name)!='' limit " . $startRecord . ",5";

$result = mysqli_query($conn, $sql2);

$sResp[] =  array();

while ($row = mysqli_fetch_assoc($result))
{
    $sRow["stid"]=$row["student_ID"];
    $sRow["lname"]=$row["last_Name"];
    $sRow["course"]=$row["course_Name"];
    $sRow["marks"]=$row['marks'];
    $sResp[] = $sRow;
}
echo json_encode($sResp);
7 để mã hóa dữ liệu của bạn thành định dạng JSON là một cách thuận tiện và được khuyến nghị.

$sql2 = "select student_ID, last_Name, course_Name, marks from students 
where trim(last_Name) is not null and trim(last_Name)!='' limit " . $startRecord . ",5";

$result = mysqli_query($conn, $sql2);

$sResp[] =  array();

while ($row = mysqli_fetch_assoc($result))
{
    $sRow["stid"]=$row["student_ID"];
    $sRow["lname"]=$row["last_Name"];
    $sRow["course"]=$row["course_Name"];
    $sRow["marks"]=$row['marks'];
    $sResp[] = $sRow;
}
echo json_encode($sResp);

Định cấu hình AJAX và gửi yêu cầu HTTP

Hàm

$sql2 = "select student_ID, last_Name, course_Name, marks from students 
where trim(last_Name) is not null and trim(last_Name)!='' limit " . $startRecord . ",5";

$result = mysqli_query($conn, $sql2);

$sResp[] =  array();

while ($row = mysqli_fetch_assoc($result))
{
    $sRow["stid"]=$row["student_ID"];
    $sRow["lname"]=$row["last_Name"];
    $sRow["course"]=$row["course_Name"];
    $sRow["marks"]=$row['marks'];
    $sResp[] = $sRow;
}
echo json_encode($sResp);
8 chỉ định các cấu hình cơ bản cho các cuộc gọi AJAX sau này.

$.ajaxSetup({
  url: 'example.php',
  type: 'post',
  
  // parameter to be sent (optional)
  // it can be sent while actually making an HTTP request
  // data: {index: myIndex}, 
  
  // set response datatype as JSON and jQuery decodes it automatically
  // that is, use the data as JS object directly
  dataType: 'json', 
  
  // successful handler
  success: displayJsonData, 
  
  // error handler
  error: printerror 
});

Tham số và giá trị

Các tham số và các giá trị tương ứng sẽ được gửi đến máy chủ có thể được xác định trong một biến:

var mydata = {
		// value from an input text box with a name="Searchname"
		mysearchname: $('input[name=Searchname]').val(),

		// value from a radio box selected by a user
		// the radio button has an attribute: name="mychoice"
		myradiobutton: $('input:radio[name=mychoice]:checked').val()
	};

Gửi yêu cầu AJAX

Khi thực sự gửi yêu cầu AJAX đến Máy chủ (PHP), hãy sử dụng chức năng

$sql2 = "select student_ID, last_Name, course_Name, marks from students 
where trim(last_Name) is not null and trim(last_Name)!='' limit " . $startRecord . ",5";

$result = mysqli_query($conn, $sql2);

$sResp[] =  array();

while ($row = mysqli_fetch_assoc($result))
{
    $sRow["stid"]=$row["student_ID"];
    $sRow["lname"]=$row["last_Name"];
    $sRow["course"]=$row["course_Name"];
    $sRow["marks"]=$row['marks'];
    $sResp[] = $sRow;
}
echo json_encode($sResp);
9. Bạn có thể vượt qua các tham số, tức là,
$.ajaxSetup({
  url: 'example.php',
  type: 'post',
  
  // parameter to be sent (optional)
  // it can be sent while actually making an HTTP request
  // data: {index: myIndex}, 
  
  // set response datatype as JSON and jQuery decodes it automatically
  // that is, use the data as JS object directly
  dataType: 'json', 
  
  // successful handler
  success: displayJsonData, 
  
  // error handler
  error: printerror 
});
0, mà bạn đã xác định sớm.

$.ajax({data:mydata});

Đối phó với phản ứng

Chức năng

$.ajaxSetup({
  url: 'example.php',
  type: 'post',
  
  // parameter to be sent (optional)
  // it can be sent while actually making an HTTP request
  // data: {index: myIndex}, 
  
  // set response datatype as JSON and jQuery decodes it automatically
  // that is, use the data as JS object directly
  dataType: 'json', 
  
  // successful handler
  success: displayJsonData, 
  
  // error handler
  error: printerror 
});
1 là một trình xử lý thành công đưa ra kết quả vào bảng với
$.ajaxSetup({
  url: 'example.php',
  type: 'post',
  
  // parameter to be sent (optional)
  // it can be sent while actually making an HTTP request
  // data: {index: myIndex}, 
  
  // set response datatype as JSON and jQuery decodes it automatically
  // that is, use the data as JS object directly
  dataType: 'json', 
  
  // successful handler
  success: displayJsonData, 
  
  // error handler
  error: printerror 
});
2.

Chúng tôi không cần phân tích dữ liệu JSON vì JQuery tự động làm điều đó cho bạn. Do đó, jQuery coi tham số

$.ajaxSetup({
  url: 'example.php',
  type: 'post',
  
  // parameter to be sent (optional)
  // it can be sent while actually making an HTTP request
  // data: {index: myIndex}, 
  
  // set response datatype as JSON and jQuery decodes it automatically
  // that is, use the data as JS object directly
  dataType: 'json', 
  
  // successful handler
  success: displayJsonData, 
  
  // error handler
  error: printerror 
});
3 là một mảng đối tượng.

function displayJsonData(myArr){
    if(myArr==null || myArr.length<=0){
    	$("#tbodyResult").empty();
	return;
    }

    var row="";
    // Note: normally i starts with 0, in this example, 
    // we start with 1
    for(var i = 1; i < myArr.length; i++) {
        row += '' + myArr[i].stid + ''; 
        row += '' + myArr[i].lname + ''; 
        row += '' + myArr[i].course + ''; 
        row += '' + myArr[i].marks + ''; 
    }
    $("#tbodyResult").html(row);
}

Phân diện là một tính năng quan trọng của các ứng dụng web để hiển thị dữ liệu khổng lồ trong các trang của các trang thay vì hiển thị tất cả các bản ghi. Với phân trang, chúng tôi chia tổng số hồ sơ thành các trang của các trang với số lượng nhỏ hồ sơ. Chúng tôi chủ yếu tải các trang phân trang trên mỗi yêu cầu trang với cùng một trang tải lại. Nhưng bây giờ, AJAX Partination được cải thiện hơn và thân thiện hơn với người dùng cho phép người dùng tải các trang tiếp theo hoặc trước mà không tải lại trang.

Vì vậy, nếu bạn là một nhà phát triển PHP và suy nghĩ để thực hiện việc phân trang AJAX tiên tiến với PHP, thì bạn sẽ ở đây ở đúng địa điểm. Trong hướng dẫn này, bạn sẽ học cách thực hiện phân trang AJAX với PHP và MySQL. Trong hướng dẫn này, chúng tôi sẽ sử dụng plugin phân trang bootstrap đơn giản để tạo phân trang trước.

Chúng tôi sẽ đề cập đến hướng dẫn này theo các bước dễ dàng để thực hiện ví dụ để tạo ra AJAX PAX nâng cao với PHP và MySQL. Chúng tôi cũng sẽ cho phép bạn tải xuống mã nguồn đầy đủ của ví dụ trực tiếp.

Hướng dẫn what is advanced ajax in php? - ajax nâng cao trong php là gì?

Ngoài ra, đọc:

  • Ajax Pagination in Codeigniter với ví dụ

Vì vậy, hãy để bắt đầu thực hiện việc phân trang AJAX Advance với PHP và MySQL. Trước khi chúng tôi bắt đầu, hãy xem cấu trúc tệp cho ví dụ này.

  • index.php
  • pagination.js
  • load_data.php

Bước1: Tạo bảng cơ sở dữ liệu MySQL

Vì chúng tôi sẽ hiển thị dữ liệu của nhà phát triển với phân trang, vì vậy trước tiên chúng tôi sẽ tạo các nhà phát triển bảng cơ sở dữ liệu MySQL để lưu trữ hồ sơ.developers to store records.

CREATE TABLE `developers` (
  `id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `skills` varchar(255) NOT NULL,
  `address` varchar(255) NOT NULL,
  `gender` varchar(255) NOT NULL,
  `designation` varchar(255) NOT NULL,
  `age` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Chúng tôi cũng sẽ chèn một vài bản ghi vào bảng nhà phát triển để hiển thị hồ sơ với phân trang.developers table to display records with pagination.

INSERT INTO `developers` (`id`, `name`, `skills`, `address`, `gender`, `designation`, `age`) VALUES
(1, 'Smith', 'Java', 'Newyork', 'Male', 'Software Engineer', 34),
(2, 'David', 'PHP', 'London', 'Male', 'Web Developer', 28),
(3, 'Rhodes', 'jQuery', 'New Jersy', 'Male', 'Web Developer', 30),
(4, 'Sara', 'JavaScript', 'Delhi', 'Female', 'Web Developer', 25),
(5, 'Shyrlin', 'NodeJS', 'Tokiyo', 'Female', 'Programmer', 35),
(6, 'Steve', 'Angular', 'London', 'Male', 'Web Developer', 28),
(7, 'Cook', 'MySQL', 'Paris', 'Male', 'Web Developer', 26),
(8, 'Root', 'HTML', 'Paris', 'Male', 'Web Developer', 28),
(9, 'William', 'jQuery', 'Sydney', 'Male', 'Web Developer', 23),
(10, 'Nathan', 'PHP', 'London', 'Male', 'Web Developer', 28),
(11, 'Shri', 'PHP', 'Delhi', 'Male', 'Web Developer', 38),
(12, 'Jay', 'PHP', 'Delhi, India', 'Male', 'Web Developer', 30);

Bước 2: Bao gồm tệp plugin bootstrap, jQuery và pagination

Vì chúng tôi sẽ sử dụng thiết kế Bootstrap và plugin plugin partination đơn giản, vì vậy trong index.php, chúng tôi sẽ bao gồm các tệp plugin bootstrap, jQuery và pagination.index.php, we will include Bootstrap, jQuery and pagination plugin files.







Bước3: Tạo bảng HTML bootstrap với thùng chứa nội dung

Trong tệp index.php, chúng tôi sẽ tạo bảng Bootstrap HTML để hiển thị các bản ghi thông qua yêu cầu AJAX. Chúng tôi cũng sẽ tạo thùng chứa phân trang để hiển thị phân trang với các nút.index.php file, we will create Bootstrap HTML table to display records through Ajax request. We will also create pagination container to display pagination with buttons.

Id Name Age Address Skills Designation

Chúng tôi cũng sẽ nhận được tổng số trang để hiển thị theo tổng số hồ sơ từ các nhà phát triển bảng cơ sở dữ liệu MySQL. Ở đây trong ví dụ này, chúng tôi sẽ hiển thị 5 bản ghi trên mỗi trang.developers. Here in this example, we will display 5 records per page.

$sql2 = "select student_ID, last_Name, course_Name, marks from students 
where trim(last_Name) is not null and trim(last_Name)!='' limit " . $startRecord . ",5";

$result = mysqli_query($conn, $sql2);

$sResp[] =  array();

while ($row = mysqli_fetch_assoc($result))
{
    $sRow["stid"]=$row["student_ID"];
    $sRow["lname"]=$row["last_Name"];
    $sRow["course"]=$row["course_Name"];
    $sRow["marks"]=$row['marks'];
    $sResp[] = $sRow;
}
echo json_encode($sResp);
0

Bước4: Tải dữ liệu phân trang với yêu cầu AJAX

Vì ở đây trong ví dụ này, chúng tôi đang xử lý giao diện với plugin Paginator đơn giản, do đó, chúng tôi sẽ xử lý yêu cầu AJAX trong Pagination. . Chúng tôi sẽ nhận được phản hồi dưới dạng dữ liệu JSON và đặt phản hồi JSON HTML để hiển thị các bản ghi phân trang.simple-bootstrap-paginator plugin, so on pageChange event, we will handle Ajax request in pagination.js to load pagination data from MySQL database by making Ajax request to server side php script load_data.php. We will get response as JSON data and set response JSON HTML to display pagination records.

$sql2 = "select student_ID, last_Name, course_Name, marks from students 
where trim(last_Name) is not null and trim(last_Name)!='' limit " . $startRecord . ",5";

$result = mysqli_query($conn, $sql2);

$sResp[] =  array();

while ($row = mysqli_fetch_assoc($result))
{
    $sRow["stid"]=$row["student_ID"];
    $sRow["lname"]=$row["last_Name"];
    $sRow["course"]=$row["course_Name"];
    $sRow["marks"]=$row['marks'];
    $sResp[] = $sRow;
}
echo json_encode($sResp);
1

Bước5: Tải bản ghi phân trang từ cơ sở dữ liệu MySQL

Trong load_data.php, chúng tôi sẽ nhận được các bản ghi từ các nhà phát triển bảng cơ sở dữ liệu MySQL theo yêu cầu dữ liệu phân trang và trả về dữ liệu dưới dạng JSON.load_data.php, we will get records from MySQL database table developers according to pagination data request and return data as JSON.

$sql2 = "select student_ID, last_Name, course_Name, marks from students 
where trim(last_Name) is not null and trim(last_Name)!='' limit " . $startRecord . ",5";

$result = mysqli_query($conn, $sql2);

$sResp[] =  array();

while ($row = mysqli_fetch_assoc($result))
{
    $sRow["stid"]=$row["student_ID"];
    $sRow["lname"]=$row["last_Name"];
    $sRow["course"]=$row["course_Name"];
    $sRow["marks"]=$row['marks'];
    $sResp[] = $sRow;
}
echo json_encode($sResp);
2

Bạn cũng có thể thích:

  • Hệ thống quản lý người dùng với PHP & MySQL
  • DataTables Thêm Chỉnh sửa Xóa với AJAX, PHP & MySQL
  • Xây dựng hệ thống trợ giúp với JQuery, PHP & MySQL
  • Xây dựng hệ thống bỏ phiếu trực tuyến với PHP & MySQL
  • Hệ thống quản lý trường học với PHP & MySQL
  • DataTables Thêm Xóa chỉnh sửa với Codeigniter
  • Tạo API RESTful bằng cách sử dụng Codeigniter
  • Xây dựng tập lệnh captcha có thể tái sử dụng với PHP
  • Lọc tìm kiếm sản phẩm bằng Ajax, PHP & MySQL
  • Tải lên hình ảnh và cắt theo phương thức với JQuery, PHP & MySQL
  • Xây dựng hệ thống thông báo đẩy với PHP & MySQL
  • Hệ thống quản lý dự án với PHP và MySQL
  • Hệ thống quản lý bệnh viện với PHP & MySQL
  • Xây dựng hệ thống bản tin với PHP và MySQL
  • Hiệu ứng tải màn hình bộ xương với AJAX và PHP
  • Xây dựng diễn đàn thảo luận với PHP và MySQL
  • Hệ thống quản lý quan hệ khách hàng (CRM) với PHP & MySQL
  • Hệ thống thi trực tuyến với PHP & MySQL
  • Hệ thống quản lý chi phí với PHP & MySQL

Bạn có thể xem bản demo trực tiếp từ liên kết demo và có thể tải xuống tập lệnh từ liên kết tải xuống bên dưới. Tải xuống demo
Demo Download

Có bao nhiêu loại ajax?

Các loại dữ liệu có sẵn là văn bản, HTML, XML, JSON, JSONP và Script.Nếu văn bản hoặc HTML được chỉ định, không có xử lý trước xảy ra.Dữ liệu chỉ đơn giản được chuyển cho trình xử lý thành công và được cung cấp thông qua thuộc tính phản hồi của đối tượng JQXHR.text , html , xml , json , jsonp , and script . If text or html is specified, no pre-processing occurs. The data is simply passed on to the success handler, and made available through the responseText property of the jqXHR object.

Mục đích của Ajax là gì?

Ajax là viết tắt của JavaScript không đồng bộ và XML.Tóm lại, đó là việc sử dụng đối tượng XMLHTTPREQUEST để giao tiếp với các máy chủ.Nó có thể gửi và nhận thông tin ở các định dạng khác nhau, bao gồm các tệp JSON, XML, HTML và văn bản.to communicate with servers. It can send and receive information in various formats, including JSON, XML, HTML, and text files.

Sự khác biệt giữa AJAX và PHP là gì?

1. AJAX là một nhóm các công nghệ cho phép các ứng dụng web truy xuất dữ liệu từ máy chủ một cách không đồng bộ;PHP là một ngôn ngữ kịch bản được thiết kế để tạo ra các trang web động.2.AJAX is a group of technologies that allows web applications to retrieve data from the server asynchronously; PHP is a scripting language designed to produce dynamic web pages. 2.

Chúng ta có thể sử dụng PHP trong AJAX không?

Nếu tôi hiểu chính xác, có bạn có thể.Đặt tất cả các chức năng của bạn trong một tệp PHP và có AJAX Pass dưới dạng tham số mà bạn muốn gọi.yes you can. Put all your functions in one php file and have the ajax pass as a parameter which one you want to call.