<!doctype html> $("button"). Click (function(){ varTypeId = $ (this). attr ("id"); vardata = {"TypeId": typeId};//Typeid={typeid} $. Get ("./a1.php", data,function(d) {varhtml = ""; for(varI in D) {HTML+ = d[i].title+ "<br/>"; } $("#box").HTML (HTML); }, "JSON"); });</script>PHP Code<?PHP//1. Connect to the databaseTry{ $dsn= "Mysql:host=localhost;dbname=jkxy"; $username= "Root"; $pwd= ""; $pdo=NewPDO ($dsn,$username,$pwd);}Catch(pdoexception$e){ Echo $e-getMessage ();}//2. Querying the databaseTry{ $typeId=$_get[' TypeId ']; $sql= "SELECT * FROM news WHERE typeId = {$typeId}"; $stmt=$pdo, Query ($sql); $news=$stmt-Fetchall (PDO::FETCH_ASSOC); EchoJson_encode ($news);}Catch(pdoexception$e){ Echo $e-getMessage ();}? >3.Build table Code CREATE TABLE ' News ' (' id ' int (One) notNULLAuto_increment,' title ' varchar (() notNULL DEFAULT‘‘,' content ' text notNULL,' typeId ' int (One) notNULL DEFAULT' 0 ',PRIMARYKEY(' id ')) ENGINE=myisam auto_increment=26DEFAULTCharset=utf8
Ajax GET Request