使用jquery .ajax从mySQL DB中检索记录

时间:2022-09-25 23:38:56

I am a mySQL noob, and have a slightly stupid question...

我是一个mySQL菜鸟,有一个稍微愚蠢的问题......

I am using jquery to send form to a php script which then stores the data in a mySQL database. On another page in my app, I need to get all the values from one column of said mySQL DB, using jquery again (I assume I will also have to write another php script) so I can use the retrieved data. I am very familiar with using POST or GET to send data, but all of a sudden realized I have no idea how to retrieve it in a way that it can be used by a jquery callback function.

我正在使用jquery将表单发送到php脚本,然后将该数据存储在mySQL数据库中。在我的应用程序的另一个页面上,我需要从所述mySQL数据库的一列中获取所有值,再次使用jquery(我假设我还必须编写另一个php脚本),以便我可以使用检索到的数据。我非常熟悉使用POST或GET发送数据,但突然意识到我不知道如何以jquery回调函数可以使用它的方式检索它。

By the way, I'm using php4.

顺便说一下,我正在使用php4。

Any help would be appreciated!

任何帮助,将不胜感激!

1 个解决方案

#1


You're looking for json_encode().

你正在寻找json_encode()。

Here is an example of using PHP, JSON and AJAX that sends JSON to PHP. This tutorial sends JSON data back.

下面是使用PHP,JSON和AJAX将JSON发送到PHP的示例。本教程将重新发送JSON数据。

By the way, are you using jQuery or similar Javascript framework? If not, I'd highly recommend it as it can abstract a lot of the cross-browser differences and error-handling with the ajax() call. Here is an example of .ajax() in action:

顺便说一句,你使用jQuery或类似的Javascript框架?如果没有,我强烈推荐它,因为它可以通过ajax()调用抽象出很多跨浏览器的差异和错误处理。以下是.ajax()的示例:

$.ajax({
    url: 'document.xml',
    type: 'GET',
    dataType: 'xml',
    timeout: 1000,
    error: function(){
        alert('Error loading XML document');
    },
    success: function(xml){
        // do something with xml
    }
});

Now all that assumes you're using PHP 5.2+ (which is when json_encode()/json_decode() were addd). If that's not the case you may have to use XML instead.

现在所有假设你都在使用PHP 5.2+(这是添加了json_encode()/ json_decode()的时候)。如果不是这种情况,您可能不得不使用XML。

Here is an introduction to Javascript and XML and the manual for the PHP XML parser. You are using jQuery so XML parsing gets a bit easier, see this article.

以下是Javascript和XML的介绍以及PHP XML解析器的手册。您正在使用jQuery,因此XML解析变得更容易,请参阅此文章。

XML is more tedious to deal with (and the messages are slightly more verbose) but it's arguably safer. For completeness it's worth pointing out the security issues with JSON.

XML处理起来比较繁琐(而且消息稍微冗长一点),但它可以说更安全。为了完整起见,值得指出JSON的安全问题。

#1


You're looking for json_encode().

你正在寻找json_encode()。

Here is an example of using PHP, JSON and AJAX that sends JSON to PHP. This tutorial sends JSON data back.

下面是使用PHP,JSON和AJAX将JSON发送到PHP的示例。本教程将重新发送JSON数据。

By the way, are you using jQuery or similar Javascript framework? If not, I'd highly recommend it as it can abstract a lot of the cross-browser differences and error-handling with the ajax() call. Here is an example of .ajax() in action:

顺便说一句,你使用jQuery或类似的Javascript框架?如果没有,我强烈推荐它,因为它可以通过ajax()调用抽象出很多跨浏览器的差异和错误处理。以下是.ajax()的示例:

$.ajax({
    url: 'document.xml',
    type: 'GET',
    dataType: 'xml',
    timeout: 1000,
    error: function(){
        alert('Error loading XML document');
    },
    success: function(xml){
        // do something with xml
    }
});

Now all that assumes you're using PHP 5.2+ (which is when json_encode()/json_decode() were addd). If that's not the case you may have to use XML instead.

现在所有假设你都在使用PHP 5.2+(这是添加了json_encode()/ json_decode()的时候)。如果不是这种情况,您可能不得不使用XML。

Here is an introduction to Javascript and XML and the manual for the PHP XML parser. You are using jQuery so XML parsing gets a bit easier, see this article.

以下是Javascript和XML的介绍以及PHP XML解析器的手册。您正在使用jQuery,因此XML解析变得更容易,请参阅此文章。

XML is more tedious to deal with (and the messages are slightly more verbose) but it's arguably safer. For completeness it's worth pointing out the security issues with JSON.

XML处理起来比较繁琐(而且消息稍微冗长一点),但它可以说更安全。为了完整起见,值得指出JSON的安全问题。