Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 11th, 2012  |  syntax: None  |  size: 2.69 KB  |  hits: 47  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to use ajax to update mysql db when checkbox condition is changed?
  2. <script type="text/javascript" src="jquery-1.2.1.min.js"></script>
  3. <script type="text/javascript">
  4.  
  5.     function checkbox_click (id, favorite)
  6.     {
  7.         // see if checkbox is checked
  8.         if(favorite==1)
  9.         {
  10.             $.ajax({
  11.                 type:'POST',
  12.                 url:'check_favorite.php', // this external php file isn't connecting to mysql db
  13.                 data:'id= ' + id + '&favorite=1',
  14.             });
  15.         }// if
  16.         // the checkbox was unchecked
  17.         else
  18.         {
  19.             $.ajax({
  20.                 type:'POST',
  21.                 url:'check_favorite.php', // this external php file isn't connecting to mysql db
  22.                 data:'id= ' + id + '&favorite=0',
  23.             });
  24.         }//else
  25.     }
  26. </script>
  27.        
  28. echo "<input type='checkbox' id='$rowid;' name='favorite' checked='checked' onclick='checkbox_click('id','favorite',this();' />";
  29.  
  30.  
  31. else
  32. echo "<input type='checkbox' id='$rowid;' name='favorite'  onclick='checkbox_click('id','favorite',this.checked);' />";
  33.        
  34. <?php
  35. //Database Variables - with the variables entered it doesn't connect
  36. $dbhost = 'localhost';   // usually localhost
  37. $dbuser = 'username';      // database username
  38. $dbpass = 'password';      // database password
  39.  
  40. //Establish connection to MySQL database
  41.  
  42. $con = @mysql_connect($dbhost, $dbuser, $dbpass);
  43. if (!$con)
  44. die('Unable to connect.' . mysql_error());
  45.  
  46. mysql_select_db('devcake', $con);
  47.  
  48. // Get the variables.
  49. $query = "UPDATE mytable SET favorite=".$_POST['favorite'] . "
  50. WHERE id=".$_POST['id'] . ";";
  51.  
  52. mysql_query($query);
  53. mysql_close($con);
  54. ?>
  55.        
  56. $dbhost='localhost';
  57. $dbuser='root';
  58. $dbpass='';
  59.        
  60. $id=$_POST['id'];
  61. $favorite=$_POST['favorite'];
  62. $query="UPDATE mytable SET favorite='$favorite' WHERE id='$id'";
  63.        
  64. echo "<input type='checkbox' id='$rowid;' name='favorite'";
  65. if(//check in database if 'favorite' column is set to 'on'){
  66. print "checked='checked'";
  67.  }
  68.     echo "/>";
  69.        
  70. $('input[name=favorite]').click(function(){
  71.     //if a checkbox with name 'favorite' is clicked, do the following.
  72.     //grab the id from the clicked box
  73.     var id=$(this).attr('id');
  74.     //grab the value from the checkbox (remember, if it is checked it will be 'on', else ''
  75.     var favorite=$(this).val();
  76.     //setup the ajax call
  77.     $.ajax({
  78.             type:'POST',
  79.             url:'check_favorite.php',
  80.             data:'id= ' + id + '&favorite='+favorite
  81.         });
  82.     }
  83.     });
  84.        
  85. echo "<input type='checkbox' id='$rowid;' name='favorite' checked='checked' onclick='checkbox_click('$rowid','favorite',this();' />";
  86.  
  87. else
  88. echo "<input type='checkbox' id='$rowid;' name='favorite'  onclick='checkbox_click('$rowid','favorite',this.checked);' />";