0

this is my html form:

<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> 

I have a table, is called: "mytable". this table contains: name(string), box(boolean).

I tried to do the next thing: when the user checked the box of don't show me again, I want to update the box of the table (assuming the row with id=6).

p.s. the box of the table inits to False.

so I have to do something like:

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

how can I do that?

I found examples with php but I don't want to do it with php.

UPDATE:

based on your answers, I want to do it with Ruby-on-rails.

I found this:

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

any help appreciated!

Alon Shmiel
  • 6,753
  • 23
  • 90
  • 138
  • 1
    You have to use some server side scripting language like php. jQuery is client side so it cannot interact with database directly. You can fire ajax to call some php script which will update database – Deadlock Feb 04 '13 at 09:41

4 Answers4

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

You can use $.ajax to update your table in database. Just create post.php file and set up a query in there which updates table.

Also, PHP uses mysqli to connect to database in latest versions. mysql_ functions are deprecated.

Muhammad Talha Akbar
  • 9,952
  • 6
  • 38
  • 62
1

Unfortunately, you need some sort of server script like php to communicate with mySQL :(

Query mySQL natively from JQuery without the need for PHP etc

Community
  • 1
  • 1
asifrc
  • 5,781
  • 2
  • 18
  • 22
1

You can use any language you like on the server (even JavaScript), but you need server side code to process the HTTP request (which you can make from JavaScript using XMLHttpRequest) and interact with the database.

(Note: "any language you like" is limited by what is installed or what you can install on your server (or a server you move to).)

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

You need to create a controller that accepts the ajax request but first update the js to this one.

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

create a controller called 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

that's the gist of it. If you can't make it work, you may need to start with more basic tutorials. Good luck!

jvnill
  • 29,479
  • 4
  • 83
  • 86