Home >Backend Development >PHP Problem >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!