Data interaction between php files and HTML pages
HTML sending (sent using POST)
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收集
php receives (collected using super-global variables $_GET and $_POST)
Copy after login
Note: Key operations
searchWrite=searchWrite=_POST["data"];
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格式
HTML receive (receive data pairs in json format returned by php echo through GET)
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); }, }); });
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!