更新表格单元格

update table cell

本文关键字:单元格 表格 更新      更新时间:2023-09-26

这是我的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> 

我有一张桌子,叫做:"神话"。该表包含:name(字符串)、box(布尔值)。

我试着做下一件事:当用户选中don't show me again的框时,我想更新表的框(假设id为6的行)。

p.s.表的框初始化为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)

感谢您的帮助!

您需要创建一个接受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

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

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

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

此外,PHP在最新版本中使用mysqli连接数据库。mysql_函数已弃用。

不幸的是,您需要一些类似php的服务器脚本来与mySQL通信:(

从JQuery本地查询mySQL,无需PHP等

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

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