What is php ajax usage?
php The usage of ajax is: 1. Splice str strings on the javascript side and send json data, with code such as "if(!(cityInfo==''&& typeInfo=='')) {$.ajax({...})"; 2. Just receive the data on the PHP side, with code such as "public function receive_search(){...}".
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.
php What is the usage of ajax?
php ajax practical (tips on using ajax)
ajax is often used in daily work projects, so I summarized one here that I use at work An example of ajax php that is often used. It is also convenient to use again in the future, and at the same time, it avoids the embarrassing situation of writing code but not knowing what you want to do. To make a long story short, just go to the code:
(Function introduction: This is an ajax php, the user selects by type, The front end displays the content the user wants)
1. JavaScript side: When splicing str strings, special attention should be paid to single and double quotation marks. It is recommended to use single quotation marks in places such as class, id, and url, and then Surround it with double quotes. Develop good habits to avoid being confused by single and double quotes. Pay special attention (the data sent is json data, so pay attention to the format to avoid unnecessary trouble)
var cityInfo=$('#cityInfo').val(); var typeInfo=$('#typeInfo').val(); if(!(cityInfo==''&& typeInfo=='')){ $.ajax({ url:'/index.php?m=member&c=index&a=receive_search', type:'POST', dataType:'json', data: {cityInfo:cityInfo,typeInfo:typeInfo}, success:function(data){ var str=''; for(i in data){ str += "<div class='caseIntroduce'>"; str += "<a href='index.php?m=member&c=index&a=exchange_show&type=accept&id="+data[i]['id']+"'>"; str += "<div class='caseinfo1'>"; str += "<span class='casetitle'>"+data[i]['title']+"...</span>"; str += "</div>"; str += "<div>"; str += "<span class='caseblue'>"+data[i]['classification']+"</span>"; str += "<span>涉案金额:"+data[i]['accountMoney']+"</span>"; str += "<span>委托费用:"+data[i]['entrustcost']+"元</span>"; str += "<span>"+data[i]['linkage']+"</span>"; str += "<span>"+data[i]['receivetime']+"</span>"; str += "</div>"; str += "</a>"; str += "</div>"; } $(".caseCenter").html(str); } }) }
2.php Receive data in the terminal (because this example is a practical implementation based on the phpcms framework; just pay attention to lines 2 and 3 to receive data, and 24 and 25 data to return)
public function receive_search(){ $cityInfo=$_POST['cityInfo']; $typeInfo=$_POST['typeInfo']; $sqlCity=$cityInfo == '' ? '' :"linkage='$cityInfo'"; $sqlType=$typeInfo == '' ? '' : "classification='$typeInfo'"; if(!empty($sqlCity)&&!empty($sqlType)){ $sql=$sqlCity.'AND '.$sqlType; }else{ $sql=$sqlCity.$sqlType; } $order = 'updatetime desc'; $catid = 181; if(!$this->set_modelid($catid)) return false; $datas = $this->db_con -> select($sql,'*', '', $order, '', ''); foreach($datas as $key=>$val){ $datas[$key]['title']=displayPart($val['title'],15); $datas[$key]['classification']=get_linkage($val['classification'],6650,'-',1); $datas[$key]['accountMoney']=get_linkage($val['accountMoney'],6766,'-',1); $datas[$key]['linkage']=sliceArea(get_linkage($val['linkage'],3360,'-',1)); $datas[$key]['receivetime']=date('Y-m-d',$val['receivetime']); } echo json_encode($datas); exit; }
Recommended learning: "PHP video tutorial》
The above is the detailed content of What is php ajax usage?. For more information, please follow other related articles on the PHP Chinese website!

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

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

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

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
