Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 关于ajax操作数据库后的数据处理问题

关于ajax操作数据库后的数据处理问题

Jun 23, 2016 pm 01:52 PM
ajax solving issues operate data database

在做一个页面,类似刷微博时拉到最下面可以选择“加载更多”,现在通过ajax实现了对数据库的操作

 function ajax() {		var page=document.getElementById("pageNum").value++;          //先声明一个异步请求对象          var xmlHttpReg = null;          if (window.ActiveXObject) {//如果是IE              xmlHttpReg = new ActiveXObject("Microsoft.XMLHTTP");          } else if (window.XMLHttpRequest) {              xmlHttpReg = new XMLHttpRequest(); //实例化一个xmlHttpReg          }          //如果实例化成功,就调用open()方法,就开始准备向服务器发送请求          if (xmlHttpReg != null) {              xmlHttpReg.open("get", "pro.php?index="+page, true);               xmlHttpReg.send(null);              xmlHttpReg.onreadystatechange = doResult; //设置回调函数          }          //回调函数          //一旦readyState的值改变,将会调用这个函数,readyState=4表示完成相应          //设定函数doResult()          function doResult() {                        if (xmlHttpReg.readyState == 4) {//4代表执行完成                                                    if (xmlHttpReg.status == 200) {//200代表执行成功                      //将xmlHttpReg.responseText的值赋给ID为resText的元素                      document.getElementById("resText").innerHTML = xmlHttpReg.responseText;                                                     }              }          }              }
Copy after login

后台数据库是php实现的,语句是
…………$sql="select * from tb_test limit ".$index*4;$result = mysql_query($sql, $con);
Copy after login

写到这儿不知道该怎么办了
通常情况下
使用while ($row=mysql_fetch_array($result)) {
……
}
就可以了,现在不知道改怎么输出,而且我的前台页面中,这些更新的数据是在一个table中


回复讨论(解决方案)

while ($row=mysql_fetch_array($result)) {
……
}
一样的在这个里面进行循环,但是此时可以加上tr等table下的标签然后组合成数组后,再转换成你要求的输出格式进行输出到结果就可以了。

将从数据库获取的数据(一般是数组),echo json_encode($res);
前台js用
var res=xmlHttpReg.responseText;
res=eval('('+res+')');
解出来
res['键名'],就可以获取对应的键值

将从数据库获取的数据(一般是数组),echo json_encode($res);
前台js用
var res=xmlHttpReg.responseText;
res=eval('('+res+')');
解出来
res['键名'],就可以获取对应的键值



然后在JS里面输出那些表格吗?

你可以像1楼说的,在php里面组织好表格(就是一个字符串),然后直接echo,js获取后不用解,直接插入到要显示的位置
因为我觉得js循环数据组织html比较麻烦(一般情况下),还不如php组织好直接输出

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Ajax: Building an autocomplete suggestion engine PHP and Ajax: Building an autocomplete suggestion engine Jun 02, 2024 pm 08:39 PM

Build an autocomplete suggestion engine using PHP and Ajax: Server-side script: handles Ajax requests and returns suggestions (autocomplete.php). Client script: Send Ajax request and display suggestions (autocomplete.js). Practical case: Include script in HTML page and specify search-input element identifier.

AI startups collectively switched jobs to OpenAI, and the security team regrouped after Ilya left! AI startups collectively switched jobs to OpenAI, and the security team regrouped after Ilya left! Jun 08, 2024 pm 01:00 PM

Last week, amid the internal wave of resignations and external criticism, OpenAI was plagued by internal and external troubles: - The infringement of the widow sister sparked global heated discussions - Employees signing "overlord clauses" were exposed one after another - Netizens listed Ultraman's "seven deadly sins" Rumors refuting: According to leaked information and documents obtained by Vox, OpenAI’s senior leadership, including Altman, was well aware of these equity recovery provisions and signed off on them. In addition, there is a serious and urgent issue facing OpenAI - AI safety. The recent departures of five security-related employees, including two of its most prominent employees, and the dissolution of the "Super Alignment" team have once again put OpenAI's security issues in the spotlight. Fortune magazine reported that OpenA

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

70B model generates 1,000 tokens in seconds, code rewriting surpasses GPT-4o, from the Cursor team, a code artifact invested by OpenAI 70B model generates 1,000 tokens in seconds, code rewriting surpasses GPT-4o, from the Cursor team, a code artifact invested by OpenAI Jun 13, 2024 pm 03:47 PM

70B model, 1000 tokens can be generated in seconds, which translates into nearly 4000 characters! The researchers fine-tuned Llama3 and introduced an acceleration algorithm. Compared with the native version, the speed is 13 times faster! Not only is it fast, its performance on code rewriting tasks even surpasses GPT-4o. This achievement comes from anysphere, the team behind the popular AI programming artifact Cursor, and OpenAI also participated in the investment. You must know that on Groq, a well-known fast inference acceleration framework, the inference speed of 70BLlama3 is only more than 300 tokens per second. With the speed of Cursor, it can be said that it achieves near-instant complete code file editing. Some people call it a good guy, if you put Curs

Astar staking principle, income dismantling, airdrop projects and strategies & operation nanny-level strategy Astar staking principle, income dismantling, airdrop projects and strategies & operation nanny-level strategy Jun 25, 2024 pm 07:09 PM

Table of Contents Astar Dapp Staking Principle Staking Revenue Dismantling of Potential Airdrop Projects: AlgemNeurolancheHealthreeAstar Degens DAOVeryLongSwap Staking Strategy & Operation "AstarDapp Staking" has been upgraded to the V3 version at the beginning of this year, and many adjustments have been made to the staking revenue rules. At present, the first staking cycle has ended, and the "voting" sub-cycle of the second staking cycle has just begun. To obtain the "extra reward" benefits, you need to grasp this critical stage (expected to last until June 26, with less than 5 days remaining). I will break down the Astar staking income in detail,

PHP vs. Ajax: Solutions for creating dynamically loaded content PHP vs. Ajax: Solutions for creating dynamically loaded content Jun 06, 2024 pm 01:12 PM

Ajax (Asynchronous JavaScript and XML) allows adding dynamic content without reloading the page. Using PHP and Ajax, you can dynamically load a product list: HTML creates a page with a container element, and the Ajax request adds the data to that element after loading it. JavaScript uses Ajax to send a request to the server through XMLHttpRequest to obtain product data in JSON format from the server. PHP uses MySQL to query product data from the database and encode it into JSON format. JavaScript parses the JSON data and displays it in the page container. Clicking the button triggers an Ajax request to load the product list.

See all articles