且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用javascript防止在Sql server DataBase中重复输入

更新时间:2023-01-22 17:14:57

如果您担心每次都不检查数据库,然后你必须在页面加载期间获取客户端的所有数据。将它们存储在javascript数组或json对象中。然后每次在字段中创建一个条目时,检查数据库中获取的数据中是否存在该特定项目。在数据库中创建条目的时间,重新加载页面以启用从数据库中获取更新的数据。然后在每次向数据库发出提交请求时检查现有数据。
If you are concerned not to check database each and every time,then you have to fetch all data in client side during page load.Store them in a javascript array or json object.Then each time you make an entry in your fields,do check for existence of that particular item in fetched data from database.Each time you make an entry in database,reload your page to enable fetching of updated data from database.Then check for existing data each time you make a submit request to database.


如果您想要在客户端,而不是在服务器端,从JQuery使用AJAX相对简单。



- 您可以进行提交按钮('接受' '或者你认为合适的任何名字)在页面加载时被禁用。



- 当用户更改输入控件中的文本时,您可以检测文本更改事件并使用AJAX(在后台)尝试下载一个text / xml来自您网站的文档。你可以使用输入控件中的文本作为查询的一部分。



- 在服务器端,当它收到文件请求时(你应该是cource)陷阱,因为它将引用不存在的页面/文档)您的代码应在数据库中检查该文本是否存在,并返回适当的内容,这些内容可能只是一个'1'或一个'0'。



- 在下载完成时触发事件的处理程序(客户端,在JQuery中捕获),您获取该值并决定启用/禁用提交按钮。





将它们放在一起很复杂,但给人的印象是全部都是在客户端完成的。



最简单的方法是使用服务器端数据验证。



这段代码可以帮助你:



在事件处理程序上放置这样的东西:



If you want to do it in the client side and not in the server side it is relatively simple to use AJAX from JQuery.

- You could make the 'Submit' button ('Accept' or whatever name you find appropiate) be disabled at page load.

- When the user changes the text in the input control you can detect the text changed event and use AJAX to (in the background) try to download one text/xml document from your site. You can use the text in the input control as part of the query.

- In the server side, when it receives the request for the file (you should of cource trap it as it will refer to non existing page/document) your code should check in the database for the presence or not of that text and return the appropiate content which may simply be one '1' or one '0'.

- On the handler for the event fired when the download has been completed (client side, captured in JQuery) you take that value and decide enabling/disabling the 'submit' button.


It is complex to put it all together but gives the impression of being all done client-side.

The simplest way to do it is with server side data validation.

This piece of code could help you:

Put something like this on the event handler:

LoadMyFile("myfiles/oneXmlFile.xml", function(xml) {
    // This variable contains the parsed xml;
    // ...do something with it here.
});





并使此功能可用于您的页面代码。当文件下载成功时,sourceDoc就是url并准备回调。





And make this function available for your page code. sourceDoc is the url and Ready the callback called when the filed download succeeds.

function LoadMyFile(sourceDoc, Ready) {


.ajax({
type: GET
url:sourceDoc,
dataType: xml
成功: function (xml){
Ready(xml);
}
});
}
.ajax({ type: "GET", url: sourceDoc, dataType: "xml", success: function(xml) { Ready(xml); } }); }





p.d.这就是我前段时间做过类似的事情,但是为你创建和测试一些代码现在可能需要很长时间。



p.d. That's how i did something similar some time ago, but creating and testing some code for you could take too long now.