Home >Backend Development >PHP Problem >How to delete data with PHP combined with AJAX?
How does PHP combine with AJAX to delete data?
First receive the GET parameters in the PHP file and delete the data according to the parameters;
$id = $_GET['id']; /*删除数据逻辑*/
Then use the function "json_encode()" to return the data;
header('Content-Type:application/json'); json_decode([ 'code' => 1, 'msg' => 'Delete success' ])
Then use AJAX to request the file on the front end;
if (window.XMLHttpRequest) { // IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码 xmlhttp=new XMLHttpRequest(); } else { // IE6, IE5 浏览器执行代码 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { alert(xmlhttp.responseText); } } xmlhttp.open("GET","del_data.php?id="+id,true); xmlhttp.send();
Finally determine whether the data returned by the request is successful.
Recommended tutorial: "PHP Tutorial"
The above is the detailed content of How to delete data with PHP combined with AJAX?. For more information, please follow other related articles on the PHP Chinese website!