您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)如何使用ajax取php頁面的數(shù)據(jù),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
1、php連接數(shù)據(jù)庫獲取數(shù)據(jù)庫的信息放入json_encode($css);{文件為:db.php}
<span style="font-size:14px;"><?php $host="localhost"; $username="root"; $password="root"; $dbName="baixing"; $port=3306; $conn=new mysqli($host,$username,$password,$dbName,$port); if(!$conn){ die("error:".$conn->connect_error); } //設(shè)置查詢結(jié)果的編碼,一定要放在query之前 $conn->query("SET NAMES 'UTF8'"); $result=$conn->query("select * from hotgoods"); //$conn->query()獲取的是二進(jìn)制 //將查詢的結(jié)果集封裝到一個數(shù)組里 $css=$result->fetch_all(); //以json的格式發(fā)送ajax的success中由data接收 echo json_encode($css); $conn->close();</span>
2、將json_encode($css)的數(shù)據(jù)放在success:function(data)中,如下:{baixing.html}
$.ajax({ type: 'POST', url: 'db.php', data:{ // "username":"admin", // "password":"123456" }, success: function (data) { var result=eval("("+data+")"); alert(result); for(var i=0;i<result.length;i++){ var str='<div class="home1">'+ '<img src="'+result[i][1]+'" alt="'+result[i][3]+'"/>'+ '<p><a href="'+result[i][2]+'" rel="external nofollow" rel="external nofollow" >'+result[i][3]+'</a></p>'+ '<div class="price">'+ '<span>¥'+result[i][4]+'</span>'+ '<del>¥'+result[i][5]+'</del>'+ ' <a href="#" rel="external nofollow" rel="external nofollow" >預(yù)定:<b>'+result[i][6]+'</b>件</a>' '</div> </div>' $(".box7 #hotSale").append(str);//追加到你需要放在的位置 } } });$.ajax({ type: 'POST', url: 'db.php', data:{ // "username":"admin", // "password":"123456" }, success: function (data) { var result=eval("("+data+")"); alert(result); for(var i=0;i<result.length;i++){ var str='<div class="home1">'+ '<img src="'+result[i][1]+'" alt="'+result[i][3]+'"/>'+ '<p><a href="'+result[i][2]+'" rel="external nofollow" rel="external nofollow" >'+result[i][3]+'</a></p>'+ '<div class="price">'+ '<span>¥'+result[i][4]+'</span>'+ '<del>¥'+result[i][5]+'</del>'+ ' <a href="#" rel="external nofollow" rel="external nofollow" >預(yù)定:<b>'+result[i][6]+'</b>件</a>' '</div> </div>' $(".box7 #hotSale").append(str); } } });
看完上述內(nèi)容,你們對如何使用ajax取php頁面的數(shù)據(jù)有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。