In PHP, ajax means "asynchronous JavaScript and XML technology" in Chinese, which refers to a web development technology for creating interactive web applications; ajax can enable asynchronous updates of web pages, which is a method that does not require Technology that can update part of a web page without reloading the entire web page.
The operating environment of this tutorial: windows7 system, javascript1.8.5&&PHP7.1 version, Dell G3 computer.
What does Ajax mean?
ajax is the abbreviation of Asynchronous JavaScript and XML (asynchronous JavaScript and XML technology), which refers to a web development technology for creating interactive web applications;
ajax Not a new programming language, but a new way of using existing standards.
ajax is a technology used to create fast dynamic web pages. By exchanging small amounts of data with the server in the background. Ajax allows web pages to be updated asynchronously. This means that parts of a web page can be updated without reloading the entire page. If traditional web pages (which do not use ajax) need to update content, the entire web page must be reloaded.
After reading the above meaning of ajax, let’s briefly take a look at the advantages and disadvantages of ajax.
Advantages of ajax:
1. The biggest point is that the page does not refresh, and the user experience is very good.
2. Use asynchronous mode to communicate with the server, with faster response capability. .
3. Some of the work previously burdened by the server can be transferred to the client, using the idle capacity of the client to process it, reducing the burden on the server and bandwidth, and saving space and broadband rental costs. And to reduce the burden on the server, the principle of ajax is to "fetch data on demand", which can minimize the burden on the server caused by redundant requests and responses.
4. Based on standardized and widely supported technology, there is no need to download plug-ins or small programs.
5. Ajax can make Internet applications smaller, faster, and more friendly.
Disadvantages of ajax:
1. Ajax does not support the browser back button.
2. Security issues AJAX exposes the details of interaction with the server.
3. The support for search engines is relatively weak.
4. Destroyed the exception mechanism of the program.
5. It is not easy to debug.
Using ajax in php
When the user types characters in the input box above, the "showHint()" function will be executed. This function is triggered by the "onkeyup" event:
<html> <head> <script> function showHint(str) { if (str.length==0) { document.getElementById("txtHint").innerHTML=""; return; } if (window.XMLHttpRequest) { // IE7+, Firefox, Chrome, Opera, Safari 浏览器执行的代码 xmlhttp=new XMLHttpRequest(); } else { //IE6, IE5 浏览器执行的代码 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("txtHint").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","gethint.php?q="+str,true); xmlhttp.send(); } </script> </head> <body> <p><b>在输入框中输入一个姓名:</b></p> <form> 姓名: <input type="text" onkeyup="showHint(this.value)"> </form> <p>返回值: <span id="txtHint"></span></p> </body> </html>
Source code explanation:
If the input box is empty (str.length==0), this function will clear the txtHint placeholder content and exit the function.
If the input box is not empty, then showHint() will perform the following steps:
Create XMLHttpRequest object
Create Function executed when the server response is ready
Sends a request to a file on the server
Please note the parameter (q) added to the end of the URL (Including the content of the input box)
PHP file
The server page called above through JavaScript is named "gethint.php" PHP file.
The source code in "gethint.php" checks the name array and returns the corresponding name to the browser:
<?php // 将姓名填充到数组中 $a[]="Anna"; $a[]="Brittany"; $a[]="Cinderella"; $a[]="Diana"; $a[]="Eva"; $a[]="Fiona"; $a[]="Gunda"; $a[]="Hege"; $a[]="Inga"; $a[]="Johanna"; $a[]="Kitty"; $a[]="Linda"; $a[]="Nina"; $a[]="Ophelia"; $a[]="Petunia"; $a[]="Amanda"; $a[]="Raquel"; $a[]="Cindy"; $a[]="Doris"; $a[]="Eve"; $a[]="Evita"; $a[]="Sunniva"; $a[]="Tove"; $a[]="Unni"; $a[]="Violet"; $a[]="Liza"; $a[]="Elizabeth"; $a[]="Ellen"; $a[]="Wenche"; $a[]="Vicky"; //从请求URL地址中获取 q 参数 $q=$_GET["q"]; //查找是否由匹配值, 如果 q>0 if (strlen($q) > 0) { $hint=""; for($i=0; $i<count($a); $i++) { if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q)))) { if ($hint=="") { $hint=$a[$i]; } else { $hint=$hint." , ".$a[$i]; } } } } // 如果没有匹配值设置输出为 "no suggestion" if ($hint == "") { $response="no suggestion"; } else { $response=$hint; } //输出返回值 echo $response; ?>
Explanation: If JavaScript sends any text (i.e. strlen($q ) > 0), then:
Find names that match the characters sent by JavaScript
If no match is found, this will respond The string is set to "no suggestion"
If one or more matching names are found, the response string is set with all names
The response is sent to the "txtHint" placeholder
PHP Ajax cross-domain problem solution
This article sets up Access -Control-Allow-Origin to achieve cross-domain.
For example: the client's domain name is client.runoob.com, and the requested domain name is server.runoob.com.
If you use ajax to access directly, there will be the following error:
XMLHttpRequest cannot load http://server.runoob.com/server.php. No 'Access-Control-Allow-Origin' header is present on the requested resource.Origin 'http://client.runoob.com' is therefore not allowed access.
1. Allow a single domain name to access
Specify a domain name (http:// client.runoob.com) for cross-domain access, you only need to add the following code to the header of the http://server.runoob.com/server.php file:
header('Access-Control-Allow-Origin:http://client.runoob.com');
2. Allow multiple domain names Visit
If you specify multiple domain names (http://client1.runoob.com, http://client2.runoob.com, etc.) for cross-domain access, you only need to access http://server. Add the following code to the head of the runoob.com/server.php file:
$origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : ''; $allow_origin = array( 'http://client1.runoob.com', 'http://client2.runoob.com' ); if(in_array($origin, $allow_origin)){ header('Access-Control-Allow-Origin:'.$origin); }
3. Allow access to all domain names
To allow access to all domain names, just http:/ Add the following code to the header of the /server.runoob.com/server.php file:
header('Access-Control-Allow-Origin:*');
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of what does ajax mean in php. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools
