What is php ajax usage?

藏色散人
Release: 2023-03-17 08:46:01
Original
1414 people have browsed it

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(){...}".

What is php ajax usage?

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=&#39;caseIntroduce&#39;>";
                        str += "<a href=&#39;index.php?m=member&c=index&a=exchange_show&type=accept&id="+data[i][&#39;id&#39;]+"&#39;>";
                        str += "<div class=&#39;caseinfo1&#39;>";
                        str += "<span class=&#39;casetitle&#39;>"+data[i][&#39;title&#39;]+"...</span>";
                        str += "</div>";
                        str += "<div>";
                        str += "<span class=&#39;caseblue&#39;>"+data[i][&#39;classification&#39;]+"</span>";
                        str += "<span>涉案金额:"+data[i][&#39;accountMoney&#39;]+"</span>";
                        str += "<span>委托费用:"+data[i][&#39;entrustcost&#39;]+"元</span>";
                        str += "<span>"+data[i][&#39;linkage&#39;]+"</span>";
                        str += "<span>"+data[i][&#39;receivetime&#39;]+"</span>";
                        str += "</div>";
                        str += "</a>";
                        str += "</div>";
                    }
                    $(".caseCenter").html(str);
                }

            })
        }
Copy after login

 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[&#39;cityInfo&#39;];
        $typeInfo=$_POST[&#39;typeInfo&#39;];
        $sqlCity=$cityInfo == &#39;&#39; ? &#39;&#39; :"linkage=&#39;$cityInfo&#39;";
        $sqlType=$typeInfo == &#39;&#39; ? &#39;&#39; : "classification=&#39;$typeInfo&#39;";
        if(!empty($sqlCity)&&!empty($sqlType)){
            $sql=$sqlCity.&#39;AND &#39;.$sqlType;
        }else{
            $sql=$sqlCity.$sqlType;
        }
        $order = &#39;updatetime desc&#39;;
        $catid = 181;
        if(!$this->set_modelid($catid)) return false;
        $datas = $this->db_con -> select($sql,&#39;*&#39;, &#39;&#39;, $order, &#39;&#39;, &#39;&#39;);
        foreach($datas as $key=>$val){
            $datas[$key][&#39;title&#39;]=displayPart($val[&#39;title&#39;],15);
            $datas[$key][&#39;classification&#39;]=get_linkage($val[&#39;classification&#39;],6650,&#39;-&#39;,1);
            $datas[$key][&#39;accountMoney&#39;]=get_linkage($val[&#39;accountMoney&#39;],6766,&#39;-&#39;,1);
            $datas[$key][&#39;linkage&#39;]=sliceArea(get_linkage($val[&#39;linkage&#39;],3360,&#39;-&#39;,1));
            $datas[$key][&#39;receivetime&#39;]=date(&#39;Y-m-d&#39;,$val[&#39;receivetime&#39;]);
        }

        echo json_encode($datas);
        exit;
    }
Copy after login

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!