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: //www.w3schools.com/js/js_json_js_intro.asp

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

Sản xuất JSON trong PHP

Mã hóa JSON trong PHP5.5: //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: //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

Bài Viết Liên Quan

Chủ Đề