Home  >  Article  >  Backend Development  >  How to delete data in php ajax

How to delete data in php ajax

藏色散人
藏色散人Original
2020-07-10 09:28:142274browse

php ajax method to delete data: first receive GET parameters in the PHP file and delete the data according to the parameters; then use the function "json_encode()" to return the data; then use AJAX to request the file on the front end .

How to delete data in php ajax

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 convert the data Return;

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();

For more related knowledge, please visit PHP Chinese website!

The above is the detailed content of How to delete data in php ajax. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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