简体   繁体   English

如何使用Ajax和Jquery从PHP数据库中提取信息,并使用该信息填充元素?

[英]How do I use Ajax and Jquery to pull information off a PHP database, and populate elements with that information?

I am tweaking a website to make it easier for employees to edit products. 我正在调整一个网站,以便员工更容易编辑产品。 Right now, someone has to login to the DB and change prices, and then someone has to change the physical html of the website itself. 现在,有人必须登录数据库并更改价格,然后有人必须更改网站本身的物理html。

So I am writing code that pulls all the products off the DB, and displays them on a page which can be edited. 所以我编写的代码将所有产品从数据库中提取出来,并将它们显示在可以编辑的页面上。 I think doing everything with Ajax would be best. 我认为用Ajax做一切都是最好的。

ajaxRequest.open("GET", url, true); ajaxRequest.open(“GET”,url,true); ajaxRequest.send(null); ajaxRequest.send(NULL);

The problem is, I only know how to process Ajax requests using a URL (using POST, GET, etc). 问题是,我只知道如何使用URL处理Ajax请求(使用POST,GET等)。 I need help writing the code to pull the info off the DB, and display it. 我需要帮助编写代码以从数据库中提取信息并显示它。

The table name is PRODUCTS. 表名是PRODUCTS。 Within PRODUCTS are the columns ID, STOCK, SHORTNAME, DESCRIPTION, PRICE and SHIPPING. 产品中包括ID,STOCK,SHORTNAME,DESCRIPTION,PRICE和SHIPPING列。

In the HTML, I have a div setup: 在HTML中,我有一个div设置:

<div class="product">

   <div class="productName">
   SHORTNAME, PRICE, SHIPPING
   </div>
   <div class="productDesc">
   ID, DESCRIPTION, STOCK
   </div>

</div>

I want to set it up so if I click a button, ajax pulls all the information off PRODUCT, and creates .productName div. 我想设置它,所以如果我点击一个按钮,ajax会从PRODUCT中提取所有信息,并创建.productName div。 If the user clicks .productName , it will then further expand to reveal .productDesc . 如果用户单击.productName ,则会进一步展开以显示.productDesc

Question: How to I query the DB using AJAX on a button click, and put the information into elements? 问题:如何在按钮单击时使用AJAX查询数据库,并将信息放入元素中?

Ajax cannot access your database. Ajax无法访问您的数据库。 Ajax simply loads content from a URL. Ajax只是从URL加载内容。 Your application must do this, and provide the result to an ajax call. 您的应用程序必须执行此操作,并将结果提供给ajax调用。

Here's how it will work. 这是它的工作方式。

  1. Browser open a url on your server 浏览器在您的服务器上打开一个URL
  2. PHP renders a page for display on the browser PHP呈现一个页面以在浏览器上显示
  3. User clicks something 用户点击了一些东
  4. Javascript sends an Ajax request to your server Javascript向您的服务器发送Ajax请求
  5. PHP recieves this request and queries the database PHP收到此请求并查询数据库
  6. PHP renders a response for the request PHP呈现请求的响应
  7. The Ajax handler on your page receives this response as plain text and does something interesting with it (such an inserting HTML, evaluating JSON, or executing javascript) 页面上的Ajax处理程序以纯文本形式接收此响应,并使用它做一些有趣的事情(例如插入HTML,评估JSON或执行javascript)

Ajax is just a way to load the response from a URL without actually navigating your browser to that page. Ajax只是一种从URL加载响应而无需实际导航浏览器到该页面的方法。 It does not (and should not) have any sort of direct database access. 它不(也不应该)具有任何类型的直接数据库访问权限。 In fact it has no idea there is a database at all. 实际上它根本不知道有一个数据库。

Imagine the security issues if any javascript could read or write any field in your database. 想象一下,如果任何javascript可以读取或写入数据库中的任何字段,则会出现安全问题。 it would be a hackers dream. 这将是一个黑客的梦想。

You have to write a PHP script that queries the database and returns the results either as JSON or HTML. 您必须编写一个查询数据库的PHP脚本,并以JSON或HTML形式返回结果。

This script is called via jQuery. 这个脚本是通过jQuery调用的。 Eg 例如

$('#main').load('http://your-url.com/yourscript.php');

This loads the output generated by yourscript.php (given that it is HTML) into an element with ID main . 这会将yourscript.php生成的输出(假设它是HTML)加载到ID为main的元素中。

You can find an Ajax tutorial with jQuery here . 你可以在这里找到一个带jQuery的Ajax教程。 And a lot more tutorials on the jQuery tutorials site . 关于jQuery教程网站的更多教程。

PHP - actually, it seems to be no job for jquery - once somebody edits it in DB, app gets latest information every hit. PHP - 实际上,似乎没有jquery的工作 - 一旦有人在数据库中编辑它,应用程序每次点击获取最新信息。

Unless I misinterpreted your question, I see nothing this has to do with jQuery/AJAX. 除非我误解了你的问题,否则我认为这与jQuery / AJAX没有任何关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM