inspireddesign Posted June 21, 2009 Share Posted June 21, 2009 Hello all, I was hoping someone could help me with the script I've put together below. This script should allow me to update my database when a checkbox is checked or unchecked? I have several checkboxes on one page and I would like to POST each Chk box to the database dynamically. I know how to make this happen using a submit button but my current solution doesn't allow for that as an option. I know that I'll need two additional files. 1) the js file for the AJAX function and 2) the php file that will do the database updates. If someone can help me get what I've put together below (at least passing the checked data to the database) I can get the other data that needs passed. As I mentioned, there will be several check boxes on the same page so I'll need an identifier to pass for that as well, which I can figure out once I get the below script working. I would very grateful for any help on this. Thanks! Calling Function: <html> <input name="chk" type="checkbox" id="chk" value="1" onclick="chkit('$_GET['uid']','chk');" /> </html> JS File: <script> function chkit(uid, chk) { var url = "check_validate.php?userid="+uid+"&chkYesNo="+chk; if(window.XMLHttpRequest) { req = new XMLHttpRequest(); } else if(window.ActiveXObject) { req = new ActiveXObject("Microsoft.XMLHTTP"); } req.open("POST", url, true); req.send(null); } </script> PHP file: <?php ////////////////////////////////////// $dbhost = 'localhost'; // usually localhost $dbuser = 'root'; // database username $dbpass = ''; // database password ////////////////////////////////////// $db = @mysql_connect($dbhost, $dbuser, $dbpass) or die ("Database connection failed."); mysql_select_db('chkdatabase'); UPDATE table_name SET chk =chkYesNo WHERE uid = userid ?> Quote Link to comment Share on other sites More sharing options...
inspireddesign Posted June 25, 2009 Author Share Posted June 25, 2009 Can someone give me a hand with this one? I can't seem to get it to work correctly. ??? Thanks. Quote Link to comment Share on other sites More sharing options...
xenophobia Posted June 26, 2009 Share Posted June 26, 2009 I'll assumed that you are using the php to generate the list of checkbox: <html> <?php while($result = mysql_fetch_array($query, MYSQL_ASSOC)) { $id = $result['id']; ?> <input name="chk" type="checkbox" id="chk_<?php echo $id; ?>" value="<?php echo $id; ?>" onclick="chkit(<?php echo $id; ?>, this.checked);" /> <?php } ?> </html> In your javascript: <script type="text/javascript"> function chkit(uid, chk) { chk = (chk==true ? "1" : "0"); var url = "check_validate.php?userid="+uid+"&chkYesNo="+chk; if(window.XMLHttpRequest) { req = new XMLHttpRequest(); } else if(window.ActiveXObject) { req = new ActiveXObject("Microsoft.XMLHTTP"); } // Use get instead of post. req.open("GET", url, true); req.send(null); } </script> In your PHP: <?php ////////////////////////////////////// $dbhost = 'localhost'; // usually localhost $dbuser = 'root'; // database username $dbpass = ''; // database password ////////////////////////////////////// $db = @mysql_connect($dbhost, $dbuser, $dbpass) or die ("Database connection failed."); mysql_select_db('chkdatabase'); // Get the variables. $userid = $_GET['userid']; $chkYesNo = $_GET['chkYesNo']; $sql = " UPDATE table_name SET chk ='$chkYesNo' WHERE uid = '$userid' "; mysql_query($sql) or die(mysql_error()); ?> Quote Link to comment Share on other sites More sharing options...
inspireddesign Posted June 26, 2009 Author Share Posted June 26, 2009 Thanks a bunch!!! This is know working. The possibilities are endless now that this works. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.