How to get data from php page with ajax

Release: 2023-02-28 07:48:01
Original
2787 people have browsed it

How to get data from php page with ajax

Nowadays, Ajax is often used to call the background php to obtain the background data. Let's take a look at how ajax obtains the data of the php page.

Recommended: php server

1. PHP connects to the database to obtain the database information and puts it into json_encode($css);{The file is: 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);
}
//设置查询结果的编码,一定要放在query之前
$conn->query("SET NAMES &#39;UTF8&#39;");
$result=$conn->query("select * from hotgoods");
//$conn->query()获取的是二进制
//将查询的结果集封装到一个数组里
$css=$result->fetch_all();
//以json的格式发送ajax的success中由data接收
echo json_encode($css);
$conn->close();</span>
Copy after login

2. Put the data of json_encode($css) in success: function(data), as follows: {baixing.html}

$.ajax({
  type: &#39;POST&#39;,
  url: &#39;db.php&#39;,
  data:{
//   "username":"admin",
//   "password":"123456"
  },
  success: function (data) {
   var result=eval("("+data+")");
   alert(result);
   for(var i=0;i<result.length;i++){
    var str=&#39;<div class="home1">&#39;+
      &#39;<img src="&#39;+result[i][1]+&#39;" alt="&#39;+result[i][3]+&#39;"/>&#39;+
      &#39;<p><a href="&#39;+result[i][2]+&#39;" rel="external nofollow" rel="external nofollow" >&#39;+result[i][3]+&#39;</a></p>&#39;+
      &#39;<div class="price">&#39;+
      &#39;<span>¥&#39;+result[i][4]+&#39;</span>&#39;+
      &#39;<del>¥&#39;+result[i][5]+&#39;</del>&#39;+
      &#39; <a href="#" rel="external nofollow" rel="external nofollow" >预定:<b>&#39;+result[i][6]+&#39;</b>件</a>&#39;
    &#39;</div> </div>&#39;
    $(".box7 #hotSale").append(str);//追加到你需要放在的位置
   }
  }
 });$.ajax({
  type: &#39;POST&#39;,
  url: &#39;db.php&#39;,
  data:{
//   "username":"admin",
//   "password":"123456"
  },
  success: function (data) {
   var result=eval("("+data+")");
   alert(result);
   for(var i=0;i<result.length;i++){
    var str=&#39;<div class="home1">&#39;+
      &#39;<img src="&#39;+result[i][1]+&#39;" alt="&#39;+result[i][3]+&#39;"/>&#39;+
      &#39;<p><a href="&#39;+result[i][2]+&#39;" rel="external nofollow" rel="external nofollow" >&#39;+result[i][3]+&#39;</a></p>&#39;+
      &#39;<div class="price">&#39;+
      &#39;<span>¥&#39;+result[i][4]+&#39;</span>&#39;+
      &#39;<del>¥&#39;+result[i][5]+&#39;</del>&#39;+
      &#39; <a href="#" rel="external nofollow" rel="external nofollow" >预定:<b>&#39;+result[i][6]+&#39;</b>件</a>&#39;
    &#39;</div> </div>&#39;
    $(".box7 #hotSale").append(str);
   }
  }
 });
Copy after login

The above is the detailed content of How to get data from php page 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!