Marvin
Marvin

Reputation: 513

HTML checkbox change value in MySQL

I have a checkbox and I need that when it is "checked" to alter a MySQL Table value to "yes" and if it's checked when you uncheck to "no".

I want to accomplish this with Ajax. How can this be done?

Upvotes: 1

Views: 1897

Answers (2)

Graviton
Graviton

Reputation: 83326

Use jQuery and PHP.

This is an example in jQuery:

 $.post("test.php", { isChecked:$('#checkbox_id').attr('checked') } );

This statement should be defined at either in the <head></head>, or in a separate JavaScript file.

And in your PHP code you can check for the isChecked POST variable:

if($_POST('isChecked')==1)
{
  //Set MySQL table to 1.
}
else
{
  //Set MySQL table to 0.
}

Here is a reference on how to use post. And here's how you can use PHP to update MySQL table.

Upvotes: 3

V a a m Y o b
V a a m Y o b

Reputation: 492

You use the checkbox's onchange event handler to issue an XmlHttpRequest to some server-side code to update your table.

Upvotes: 1

Related Questions