如何使用JavaScript来查询数据库? [英] How to query database using javascript?

查看:216
本文介绍了如何使用JavaScript来查询数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由新手另一个问题。我有一个在数据库中查询值的PHP变量。它存储在变量$发布和它的值将改变(在数据库中)时,用户点击超链接。

Another question by a newbie. I have a php variable that queries the database for a value. It is stored in the variable $publish and its value will change (in the database) when a user clicks on a hyperlink.

if ($publish == '') { 
Link to publish.html
} else { 
Link to edit.html
} 

什么是发生在后台,我查询数据库表,我存储在$公布变量的一些数据。如果$发布是空的,它将增加在弹出的链接publish.html。弹出将处理的格式,并将所述数据添加到数据库中和,这意味着$发布没有更多的空。什么我想实现的是,只要形式在弹出处理和数据已被添加到数据库,链接应该改变到edit.html。发生这种情况时,该页面将重新查询数据库,但它应该发生没有刷新页面。

What is happening in the background is i am querying a database table for some data that i stored in the $publish variable. If the $publish is empty, it will add a link for publish.html in a popup. The popup will process a form and will add the data to the database and which means that the $publish is no more empty. What i would like to achieve is that as soon as the form is processed in the popup and a data has been added to the database, the link should change to edit.html. This can happen when the page will re-query the database but it should happen without page refresh.

怎样才可以使用JavaScript和jQuery或AJAX donw?请帮助。

How can it be donw using javascript, jquery or ajax?? Please assist.

推荐答案

Java脚本本身不能被用来处理数据库。这是通过使用PHP(或您所选择的服务器端语言)。阿贾克斯用于将请求发送到您的PHP脚本使用JavaScript,这将又与数据库进行通信。而且它不需要刷新页面。

Javascript by itself cannot be used to deal with database. That is done using php (Or the server side language of your choice). Ajax is used to send a request to your php script using javascript which will in turn communicate with the db. And it doesn't require a page refresh.

所以,你正在尝试做可以很容易地实现用ajax什么。既然你提到的jQuery,你可以检查出$阿贾克斯或$。员额方法的jQuery这使得过程更加简单。

So what you are trying to do can be easily achieved using ajax. Since you mentioned jquery, you can check out the $.ajax or $.post methods in jquery which make the process even more simple.

您需要使用AJAX来处理表单。 Ajax请求被发送到一个PHP脚本,这将使得在数据库中进行必要的修改和发送响应新的链接(链接到edit.html)。一旦得到回应,只是用新的替换当前锚元素。

You need to process the form using ajax. The ajax request is sent to a php script which will make the necessary changes in the database and send the new link (link to edit.html) in the response. Upon getting the response, just replace the current anchor element with the new one ..

有关,例如..

$.post(url, formdataobject , function (resp) {
   $("a.youra").text('edit').attr('href', resp);
});

网​​址 - 在PHP脚本位于

url - where the php script is located

formdataobject - 这将有形式的数据作为键值对的JavaScript对象

formdataobject - a javascript object that will have the form data as key value pairs

第三个参数是匿名函数也称为回调函数,因为只有当从服务器接收到响应它会被调用。这是因为Ajax请求是异步的。

the third parameter is an anonymous function also known as callback function since it will be invoked only when the response is received from the server. This is because ajax requests are asynchronous.

里面的回调函数,jQuery是用来锚元素中的文字更改为修改的href 属性更改为值排在了回应。

Inside the callback function, jquery is used to change the text inside the anchor element to edit and the href attribute is changed to value that came in the response.

$。后表示。这样的参数也可以访问$ _POST数组在PHP中的元素。 更新数据库后,你可以简单地回声出了新的链接,这将是在响应接收。

$.post means we are using the post method. so the parameters can be accessed as elements of $_POST array in php. After updating the db, you can simply echo out the new link and it will be received in the response.

另外,还有一些其他的格式,你可以得到如响应。 XML,JSON。

Also, there are other formats in which you can get the response for eg. xml, json.

这篇关于如何使用JavaScript来查询数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆