将 jQuery 与 SQL Server 数据库连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6002031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:12:20  来源:igfitidea点击:

Connecting jQuery with the SQL Server Database

sql-serverjquery-uijquery

提问by Cipher

I am trying to build a jQuery functionality for which the scenario is given below.

我正在尝试构建一个 jQuery 功能,其场景如下所示。

Consider a user clicks on an image in the website, a jQuery dialogue box pops up on the page. The dialogue box has a text field to enter the 'alternative text' for the image. When the user clicks on the submit button, the text from this page should get saved into my central website's database along with the url of the image.

考虑用户单击网站中的图像,页面上弹出一个 jQuery 对话框。该对话框有一个文本字段,用于输入图像的“替代文本”。当用户单击提交按钮时,该页面的文本应该与图像的 url 一起保存到我的中央网站的数据库中。

I have been able to get the jQuery dialogue box working (by going step and step in jQuery) with the following code http://pastebin.com/nxALvAPP

我已经能够使用以下代码使 jQuery 对话框工作(通过在 jQuery 中一步一步地进行)http://pastebin.com/nxALvAPP

But I wanted the help to save the 'image text' and it's url in the database from. How can that be done? I am not really a pro.

但我想要帮助保存“图像文本”和它在数据库中的网址。那怎么办呢?我不是真正的专业人士。

Thanks for help!

感谢帮助!

回答by Joshua - Pendo

You'd have to pull an ajax request to a php page with the values of the fields as data. Take a look at http://api.jquery.com/jQuery.ajax/

您必须将 ajax 请求拉到一个 php 页面,并将字段的值作为数据。看看http://api.jquery.com/jQuery.ajax/

As you've came so far, I don't believe this causes any problems for you.

既然你已经走了这么远,我相信这不会给你带来任何问题。

///////////////////

/////////////////////

Basically what you do is:

基本上你要做的是:

add an click-event to the submit button:

向提交按钮添加点击事件:

$('#button_id').click(function(e) { 
  e.preventDefault();
  // button action
});

within this action, you call up a file and add data, for example:

在此操作中,您调用一个文件并添加数据,例如:

$.ajax({
  url: "savetext.asp",
  context: document.body,
  data: 'title='+$('#title').val(),
  success: function(){
    alert('ajax file called');
  }
});

This sends a request to savetext.asp with data 'title' (which has the value of the input field with the id title). I'm not sure what's the equivalent of PHP's $_REQUEST but this parameter (title) is send in the URL to the file savetext.asp.

这会向 savetext.asp 发送一个带有数据“title”的请求(它具有带有 id 标题的输入字段的值)。我不确定 PHP 的 $_REQUEST 的等价物是什么,但此参数(标题)在 URL 中发送到文件 savetext.asp。

回答by Joshua - Pendo

jQuery doesnt have any facility to interact with your SQL Server.

jQuery 没有任何与您的 SQL Server 交互的工具。

You will have to add a button or something that submits to some middleware ASP.Net, classic ASP or even PHP.

您将不得不添加一个按钮或提交到某些中间件 ASP.Net、经典 ASP 甚至 PHP 的东西。

Then server side you take what jQuery has sent and issue a sql statement to update.

然后服务器端接收 jQuery 发送的内容并发出 sql 语句进行更新。

As a terrible terrible example:

作为一个可怕的例子:

$('#save-button').click(function() {
 var id = $('img.active-pic').data('id') // if using data-id="33"
 var altText = $('.alt-textbox').val()

 $.ajax({
   method: 'POST',
   url: '/ASPNetApplication1/UpdateAltText.aspx',
   data: { id: id, altText: altText }
   success: function() {
     alert("yeah it worked")
   }

 }
)
}