How html interacts with php data

Release: 2023-02-27 22:52:01
Original
3867 people have browsed it

How html interacts with php data

Data interaction between php files and HTML pages

HTML sending (sent using POST)

    无标题文档 
Copy after login

Note: Key operations

var request = new XMLHttpRequest(); request.open("POST", "servertest.php"); var q = "data=" + writeArray;// 生成信息体q = “name “+ value request.setRequestHeader("Content-type","application/x-www-form-urlencoded"); request.send(q); //HTML页面POST发送内容后,php通过超全局变量 $_GET 和 $_POST收集
Copy after login

php receives (collected using super-global variables $_GET and $_POST)


        
Copy after login

Note: Key operations

searchWrite=searchWrite=_POST["data"];
Copy after login

Use super-global variables $ _POST collects the value corresponding to the name and puts it into $searchWrite, so the data sent by the HTML page is obtained, which can be used

HTML page to obtain the php variables through json

php Send (return data pairs in json format through echo)


        
Copy after login

Note: Key operations

$result = '{"success":true,"defaultSearch":"'.$defaultSearch.'"}';//将待返回内容改为json格式 echo $result;//HTML页面的json部分将从echo的输出获取json格式化数据对,因此echo输出内容需要为json格式
Copy after login

HTML receive (receive data pairs in json format returned by php echo through GET)

Copy after login

Key operations:

$.ajax({    type: "GET", url: "default_search.php?data=" + "searchArray", dataType: "json", success: function(data) {    If(data.success){alert(data.defaultSearch);} //data为php使用echo输出的json格式的数据对,通过data.name的形式即可以使用name对应的value }, error: function(jqXHR){   alert("发生错误:" + jqXHR.status); }, }); });
Copy after login

The above is the detailed content of How html interacts with php data. 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
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!