dennismonsewicz Posted October 3, 2008 Share Posted October 3, 2008 Is there anyway to assign the value of a checkbox according to if the user clicks on a checkbox or not? Like if a user clicks on the checkbox then its value will equal 1 if it is unchecked then it will equal 0 Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 3, 2008 Share Posted October 3, 2008 If you check a checkbox, it returns the value on submit. If it is not checked, it does not return the value. If it's not checked, it may as well not exist. Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 3, 2008 Author Share Posted October 3, 2008 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!'; } ?> Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 3, 2008 Share Posted October 3, 2008 List more of your code. Just assign a variable to 1 if your checkbox is set, and 0 if it's not. Quote Link to comment Share on other sites More sharing options...
Flames Posted October 3, 2008 Share Posted October 3, 2008 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. Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 3, 2008 Share Posted October 3, 2008 Yes. That is what I was saying. List more of your code. Just assign a variable to 1 if your checkbox is set, and 0 if it's not. Quote Link to comment Share on other sites More sharing options...
Flames Posted October 3, 2008 Share Posted October 3, 2008 I know i just thought i'd give an example of it. Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 3, 2008 Author Share Posted October 3, 2008 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' Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 3, 2008 Share Posted October 3, 2008 Looks like you have more than one checkbox named "checkboxes." Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 3, 2008 Author Share Posted October 3, 2008 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; } ?> Quote Link to comment Share on other sites More sharing options...
Flames Posted October 3, 2008 Share Posted October 3, 2008 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. Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 3, 2008 Share Posted October 3, 2008 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? 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.