Jump to content

Checkboxes - Update Database on ckecked or unchecked


inspireddesign

Recommended Posts

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

?>

Link to comment
Share on other sites

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());
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.