Home Backend Development PHP Problem what does ajax mean in php

what does ajax mean in php

Mar 10, 2022 pm 03:04 PM
ajax php

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.

what does ajax mean in php

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>
Copy after login

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;
?>
Copy after login

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 &#39;Access-Control-Allow-Origin&#39; header is present on the requested resource.Origin &#39;http://client.runoob.com&#39; is therefore not allowed access.
Copy after login

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(&#39;Access-Control-Allow-Origin:http://client.runoob.com&#39;);
Copy after login

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[&#39;HTTP_ORIGIN&#39;])? $_SERVER[&#39;HTTP_ORIGIN&#39;] : &#39;&#39;;  
  
$allow_origin = array(  
    &#39;http://client1.runoob.com&#39;,  
    &#39;http://client2.runoob.com&#39;  
);  
 
if(in_array($origin, $allow_origin)){  
    header(&#39;Access-Control-Allow-Origin:&#39;.$origin);       
}
Copy after login

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(&#39;Access-Control-Allow-Origin:*&#39;);
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles