【问题标题】:update table cell更新表格单元格
【发布时间】:2013-02-04 09:36:13
【问题描述】:

这是我的 html 表单:

<form name="input" action="html_form_action.asp" method="get">
   <input type="checkbox" id="mycheckbox">Don't show me again<br>
   <input type="submit" id="Submit">
</form> 

我有一个表,叫做:“mytable”。此表包含:名称(字符串)、框(布尔值)。

我尝试做接下来的事情: 当用户选中don't show me again的框时,我想更新表格的框(假设id=6的行)。

附言表格的框初始为 False。

所以我必须这样做:

$("#submit").click(function() {
    if($("#mycheckbox").is(':checked')) {
        //here I have to update the table in the database
    }
});

我该怎么做?

我找到了 php 的例子,但我不想用 php 来做。

更新:

根据你的回答,我想用 Ruby-on-rails 来做。

我发现了这个:

update_sql = "update mytable set box = TRUE where id = 6"
affected_rows = Job.connection.update(update_sql)

任何帮助表示赞赏!

【问题讨论】:

  • 您必须使用一些服务器端脚本语言,例如 php。 jQuery 是客户端,因此它不能直接与数据库交互。您可以触发 ajax 来调用一些将更新数据库的 php 脚本

标签: javascript jquery html ruby-on-rails json


【解决方案1】:

你需要创建一个控制器来接受 ajax 请求但首先更新 js 到这个。

# js
$("#submit").click(function() {
  $.ajax({
    url: '/my_tables/' + <id of resource to update>,
    type: 'PUT',
    data: { my_table: { box: $("#mycheckbox").is(':checked') } }
  });
});

创建一个名为MyTablesController的控制器

# my_tables_controller.rb
class MyTablesController < ActionController::Base
  def update
    @my_table = MyTable.find params[:id]
    @my_table.update_attributes params[:my_table]
    # do something else here like a redirect ...
  end
end

这就是它的要点。如果你不能让它工作,你可能需要从更基础的教程开始。祝你好运!

【讨论】:

    【解决方案2】:
    $("#submit").click(function() {
        if($("#mycheckbox").is(':checked')) {
            $.ajax({
                 url: 'post.php',
                 cache: false,
                 success: function() {
                      alert("done");
                 }
            });
        }
    });
    

    您可以使用$.ajax 更新数据库中的表。只需创建post.php 文件并在其中设置一个更新表的查询。

    另外,PHP 使用 mysqli 连接到最新版本的数据库。 mysql_ 函数已弃用。

    【讨论】:

      【解决方案3】:

      不幸的是,您需要某种服务器脚本(例如 php)来与 mySQL 进行通信 :(

      Query mySQL natively from JQuery without the need for PHP etc

      【讨论】:

      • 没问题,我们是来帮忙的 :)
      【解决方案4】:

      您可以在服务器 (even JavaScript) 上使用您喜欢的任何语言,但您需要服务器端代码来处理 HTTP 请求(您可以使用 XMLHttpRequest 从 JavaScript 发出)并与数据库交互。

      (注意:“您喜欢的任何语言”受安装的内容或您可以在服务器(或您移动到的服务器)上安装的内容的限制。)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-01-05
        • 1970-01-01
        • 2017-06-08
        • 2020-10-13
        • 2021-12-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多