Home > Backend Development > PHP Problem > How to delete data with PHP combined with AJAX?

How to delete data with PHP combined with AJAX?

Guanhui
Release: 2023-03-01 16:58:01
Original
2274 people have browsed it

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'];

/*删除数据逻辑*/
Copy after login

Then use the function "json_encode()" to return the data;

header('Content-Type:application/json');
json_decode([
	'code' => 1,
	'msg' => 'Delete success'
])
Copy after login

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();
Copy after login

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!

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