Home > front end >  PHP database query returns json data, how to extract the corresponding ajax attribute?
PHP database query returns json data, how to extract the corresponding ajax attribute?

Time:09-17

 
<? PHP
The header (" content-type: text/HTML; charset=utf-8");
Include (' conn. PHP);
='select id, $SQL article_name from article';
$res=mysqli_query ($conn, $SQL);
$data=(https://bbs.csdn.net/topics/array);
While ($row=mysqli_fetch_assoc ($res)) {
$data []=$row;
}
Echo json_encode ($data);
?>


 
Name and id//home page article
The $(document). Ready (function () {
$. Ajax ({
Url: "./active/PHP. PHP ",//the background request data, using PHP
Type: "post",//request
Async: false,//whether the asynchronous request
Success: the function (data) {//if the request is successful, return data,
Var res=JSON. Parse (data);
console.log(res);
The console. The log (res [' id ']);
Var HTML="";
for(let i=0; iHTML +="& lt; Div> \
Div style
";
}
$(" # tw_xinxi "). Append (HTML);
},
})
})


The console. The log (res) output for

Array (2)
0: {id: "1", article_name: "4124214"}
1: {article_name id: "3" : "666657"}
Length: 2
__proto__ : Array (0)

The console. The log (res) [r]. 0 or 1 id) and the console log (res) [r]. 0 or 1 article_name) to obtain the corresponding id and article_name, if the database to add new data, corresponding attribute query need to write the console. The log (res array [representing] id), manually added to the code, is a BUG, how to obtain corresponding id and article_name automatically, regardless of the database to add or delete the data?
  •  Tags:  
  • Ajax
  • Related