简体   繁体   English

是否可以使用Javascript插入或删除数据库记录?

[英]Is it possible to insert or delete a database record using Javascript?

I'm new to Javascript and I couldn't find this information so far. 我是Java语言的新手,到目前为止找不到此信息。

I'd like to create a button which, when clicked, creates a new entry in my database, and another one which removes a specific entry when clicked. 我想创建一个按钮,单击该按钮可在数据库中创建一个新条目,而另一个按钮则可在单击时删除特定条目。 The reason I don't want to use PHP is because I don't want the page to reload when the button is clicked. 我不想使用PHP的原因是因为我不希望单击按钮时重新加载页面。

Where can I find information on how to achieve this? 在哪里可以找到有关如何实现此目标的信息?

EDIT 编辑

Okay I found this: 好吧,我发现了这一点:

<script type="text/javascript">
    $(document).on('click','#save',function(e) {
        var data = $("#save-form").serialize();
        $.ajax({
            data: data,
            type: "post",
            url: "save.php",
            success: function(data){
                alert("Data Save: " + data);
            }
        });
    });
</script>

My HTML: 我的HTML:

<form id="save-form" method="post">
    <input id="save" type="submit" value="Save" name="save-submit" />
</form>

My PHP file (save.php), which creates a new record in my database, works, but only if my form's action is action="save.php" . 我的PHP文件(save.php)在数据库中创建了一条新记录,但是可以工作,但action="save.php"是表单的actionaction="save.php" What should I put in my form's action to trigger the script? 我应该在表单action以触​​发脚本?

Also, it seems like the submit button still reloads the page (even if my form has no action ). 另外,似乎“提交”按钮仍会重新加载页面(即使我的表单没有任何action )。

You cannot directly communicate with Database from your javascript . 您无法通过javascript直接与Database通信。 What you can do is like use Ajax to communicate with your server side language(It can be anything like PHP,.net,Java etc..) and with the help of server side language communicate with Database 您可以做的就像使用Ajax与您的服务器端语言进行通信(可以是PHP,.net,Java等。),并借助服务器端语言与Database通信。

If you are using Ajax then you can make the way asynchronous . 如果使用的是Ajax则可以使方法asynchronous

If you really want use JavaScript to communicate with Database then you can go for Node.js (There you can use javascript syntax) but still it is a server side language based on Javascript and its not related to your browser 如果您确实想使用JavaScript与数据库进行通信,则可以使用Node.js (可以使用javascript语法),但是它仍然是基于Javascript的服务器端语言,并且与您的浏览器无关

Why you cannot connect database from client side ?? 为什么不能从客户端连接数据库?

The main reason is Security . 主要原因是Security You're not granting DB access to anyone but web server/app user. 您不向除web server/app user.任何人授予数据库访问权限web server/app user.

The other few reason are 其他几个原因是

DB load reduction

Scalability  Issues 

Encapsulation Issues 

Ability for fault tolerance

See there are ways to insert into db with using PHP and without doing a postback. 请参阅使用PHP且不进行回发即可插入db的方法。 But specific to your question you can find answer in below link 但是针对您的问题,您可以在下面的链接中找到答案

Insert Record in Database Using Textboxes in JavaScript 使用JavaScript中的文本框在数据库中插入记录

Not only in PHP but also in any other programming language(eg: asp.net,java etc) you can create an entry and remove a specific entry without loading the page. 不仅可以使用PHP,还可以使用任何其他编程语言(例如:asp.net,java等)创建条目并删除特定条目,而无需加载页面。 You need to use the plugin of jquery which is called 'ajax'. 您需要使用名为“ ajax”的jquery插件。

Using 'ajax' you can send data to the server without loading the page. 使用“ ajax”,您可以在不加载页面的情况下将数据发送到服务器。 And after having your data in server side, you can do whatever you want to do. 在服务器端拥有数据之后,您可以做任何想做的事情。

You will find ajax documentation here: http://api.jquery.com/jquery.ajax/ 您可以在这里找到ajax文档: http : //api.jquery.com/jquery.ajax/

Same kind of question answered here: Delete record without refresh page using php 在此处回答的同类问题: 使用php删除没有刷新页面的记录

If you having any problem,let me know. 如果您有任何问题,请告诉我。

在此处输入图片说明

Here is an example of a simple client server communication model. 这是一个简单的客户端服务器通信模型的示例。 Client codes are executed at the browser which is JavaScript. 客户端代码在JavaScript浏览器中执行。 The Server side codes are PHP, Nodejs, java, python etc) Which resides on server only. 服务器端代码是PHP,Nodejs,java,python等),它们仅驻留在服务器上。 Client sends a request to the server for a resource. 客户端向服务器发送资源请求。 And server and back a response with an HTTP status. 然后服务器返回具有HTTP状态的响应。 Which represents the status of a response. 代表响应状态。 The client side JavaScript cannot communicate directly with the server database. 客户端JavaScript无法直接与服务器数据库通信。 What it does is sending a request to the server which will eventually trigger the actual code in server which inserts the data into the server. 它所做的是向服务器发送请求,该请求最终将触发服务器中的实际代码,该代码会将数据插入服务器。

The one you mentioned is the ajax method which sends an HTTP request without actually reloading a page. 您提到的一个是ajax方法,该方法发送HTTP请求而不实际重新加载页面。 You can also use a form to post data into the server without ajax which will reload the page. 您也可以使用表单将数据发布到不带ajax的服务器中,而ajax将重新加载页面。

You must validate the user inputs in server side even though you will be validating it in the client side. 即使您要在客户端验证用户输入,也必须在服务器端验证用户输入。

You can use the event.preventDefault() to prevent the form submit and then insert it via ajax. 您可以使用event.preventDefault()阻止表单提交,然后通过ajax将其插入。

$(document).ready(function() {
  $('#save-form').on('submit', function(e){      
      e.preventDefault();
       var data = $("#save-form").serialize();
        $.ajax({
            data: data,
            type: "post",
            url: "save.php",
            success: function(data){
                alert("Data Save: " + data);
            }
        });
  });
});

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

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