Jump to content

controlling the value of a checkbox using javascript


dennismonsewicz

Recommended Posts

well the reason I was wondering is because when a user decides they want to edit an entry they receive a screen where it has the data displayed from the DB in front of them. A piece of the editable data is a checklist (where in the DB a 1 == checked; 0 == unchecked) I need it when they click on an item that is checked it will return a zero and update the database when they click the submit button.

 

<?php

if($chkbx) {
foreach($chkbx as $key=>$val) 
{
	  $data_name .= $key . "=" . $val . ",";

}

$trimmedvar = trim($data_name, ",");

$qry = mysql_query("UPDATE has_had_projects SET project = '$project_name', $trimmedvar WHERE project = '$project_name'")or die(mysql_error());
echo 'Testing chkbxs works!';
} 		

?>

Link to comment
Share on other sites

couldn't use just use isset?

 

e.g.

 

HTML
<html>
<form action="php" method ="post">
<input type="checkbox" name="test" value="1" />
<input type="submit" />
</form>
</html>

PHP
<?php
if(isset($_POST['test']))
$variable = $_POST['test'];
else
$variable = 0;

wouldn't that have the same effect.

Link to comment
Share on other sites

here is my modified code with an if statement

 

 <?php
if(isset($_POST['checkboxes'])) {
$var = $_POST['checkboxes'];
} else {
$var = 0;
}

if(isset($chkbx)) {
foreach($chkbx as $key=>$val) {
          $data_name .= $key . "=" . $var . ",";
}

$trimmedvar = trim($data_name, ",");

$qry = mysql_query("UPDATE has_had_projects SET project = '$project_name', $trimmedvar WHERE project = '$project_name'")or die(mysql_error());
echo 'Testing chkbxs works!';
}	?>		

 

Its not working! I get the following error msg

 

Unknown column 'Array' in 'field list'

Link to comment
Share on other sites

here is the list of checkboxes:

 

<?php

$checkbox_qry = mysql_query("SELECT * FROM has_had_projects WHERE project = '" . $results->project . "'")or die(mysql_error());
$field = mysql_num_fields($checkbox_qry);
while($row = mysql_fetch_assoc($checkbox_qry)) {													
	for($i = 2; $i < $field; $i++) {
		$names = mysql_field_name($checkbox_qry, $i);	
		$chk = $row[$names]==1?'checked="checked"':'';
		$numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);																	
$title .= '<div><input type="checkbox" name="checkboxes[' . $names . ']" class="checkbox" id="' . $names . '" ' . $chk . ' /> <label for="checkboxes[' . $names . ']">' . ucwords(str_replace($numbers, '', $names)) . '</label></div>';
}
echo $title;
}

?>

Link to comment
Share on other sites

i think you might as well repost this in php help & if you do try and explain the php more. Is it all one page? Is there any more code?

 

Im not sure about other people but i cant figure it out using the code you've put there but know this, many people are experts in php who can fix your error in a second but don't check the JS forum.

Link to comment
Share on other sites

Flames is right, this really should be in the PHP help board, but your problem is that you are passing an array of values through POST, and then expecting PHP to know what you want to do with the multiple values.

 

Basically, at one spot you're saying that "checkboxes equals 1, 3, 10, 34, 2, and 17" and at another spot you're saying "if checkboxes equals 'david' do this, otherwise do something else." PHP sees 1, 3, 10, 34, 2, and 17 and you're testing if for something else.

 

That make sense? You need to treat an array as an array.

 

I thought we covered this in another topic with the exact same code?

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.