0

I am trying to accomplish live checkbox result with checked/unchecked checkbox. My logic for checkbox works but now I want to store this live checkbox result in MYSQL. So when I click on checkbox, result should be stored in database same as when I uncheck checkbox result should be stored in database. I don't want to use any button in this example.

Here is my code:

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script type="text/javascript" >
    function writeTo(object) {
      var container = document.getElementById("container");
      if (object.checked) {
         container.innerHTML =  "Added " + object.value + " <br />";
         } else {
        container.innerHTML =  "Removed " + object.value + " <br />";

      }
    }
</script>
</head>
<body>

    <div id="container"></div>
    <input type="checkbox" onclick="return writeTo(this)" name="check_list" value="Appel">Apple<br>
    <input type="checkbox" onclick="return writeTo(this)" name="check_list" value="Grape">Grape<br>
    <input type="checkbox" onclick="return writeTo(this)" name="check_list" value="Orange">Orange<br>
    <?php
   echo $dd= "<script>document.writeln(container.innerHTML);</script>";

require 'Database.php';

echo $sql="Update scott123.rahul_tbl_users set group1='$dd' where Dingoid=70001501";
//$sql1=mysql_query($sql);
     ?>

</body>
</html>
Fabio
  • 23,183
  • 12
  • 55
  • 64

2 Answers2

0

Have your writeTo function initiate an ajax request to your server that performs the mysql insert/update query.

You can check the current state of a checkbox by reading its "checked" attribute.

If you have experience with jQuery, it makes a lot of this easier to implement.

  • Hello i tried with "checked" attribute it works but i want to store the checked/unchecked value in database so this doent works for me.And if you are using jquery we cannot pass value to php,mysql.Thanks – Rahul Deshmukh May 15 '13 at 13:05
0

In your writeTo function, you'll have to perform an Ajax call to a script that will then update the database.

In most basic form, it would look like: (using jQuery - http://jquery.com/)

function writeTo(object) {
    // .. your other code ..
    $.ajax( {
        url: 'update.php',
        method: 'POST',
        data: { chkvalue: object.value }
    } );
}

And update.php could then look like:

<?php
    $value = $_POST['chkvalue'];
    // $value now contains the checkbox value, which can now be updated in DB
matthiasmullie
  • 2,063
  • 15
  • 17