rachae1 Posted September 14, 2007 Share Posted September 14, 2007 Hi, I have been trying to learn checkboxes and arrays, and I've finally figured out how to add the checkboxes to the database. I currently have two forms addlinks.php and modifylinks.php and I have a dolinks.php that submits my data to the database. What I would like to do now is on my modifylinks page show a tick in the boxes to show that they have already been added. At the moment if I dont tick any boxes on modifylinks.php it deletes my previous data, and I have to retick them everytime I do an update. heres some code: addlinks.php <input type="checkbox" name="category[]" value="apple">apple <input type="checkbox" name="category[]" value="oranges">oranges dolinks.php if ($submit == 'Add') { if ($linkName == ""){ echo forwardScript($href); exit; } else { $category = '*'.implode('*',$_POST['category']).'*'; $links_query = "INSERT INTO link "; $links_query .= "(category, name, description) "; $links_query .= "VALUES ("; $links_query .= "'" . $category . "',"; $links_query .= "'" . $name . "',"; $links_query .= "'" . $description . "'"; $links_query .= ");"; $link_query_result = domysql($links_query, 'Adding a link' . $_SERVER['PHP_SELF']); } } if ($_POST['submit'] == 'Modify') { if ($linkName == ""){ echo forwardScript($href); exit; } else { $category = '*'.implode('*',$_POST['category']).'*'; $links_query = "UPDATE link SET "; $links_query .= "category ='" . $category . "',"; $links_query .= "name ='" . $name . "',"; $links_query .= "description ='" . $description . "'"; $links_query .= "WHERE id LIKE '" . $linkid . "';"; $link_query_result = domysql($links_query, 'Modifying a link' . $_SERVER['PHP_SELF']); } } Quote Link to comment https://forums.phpfreaks.com/topic/69352-checkboxes/ Share on other sites More sharing options...
phat_hip_prog Posted September 14, 2007 Share Posted September 14, 2007 A wrapper: function helpers_form_checkbox_s($name, $checked) { if ($checked == 0) { $checked = ""; } else { $checked = "checked"; } return "<input type=\"checkbox\" name=\"".$name."\" ".$checked.">"; } Quote Link to comment https://forums.phpfreaks.com/topic/69352-checkboxes/#findComment-348447 Share on other sites More sharing options...
chronister Posted September 14, 2007 Share Posted September 14, 2007 Here is a function on how I do it... although I may start using the method phat_hip_prog showed. It's pretty cool. But I digress. Here is my code <?php connectdb_com(); // make connection $query="SELECT * FROM ammenities ORDER BY ammenity ASC"; //setup query $result=mysql_query($query);//run query while($row=mysql_fetch_object($result)) //start loop to get results { if(in_array($row->ammenityid,$_ammenities)) { // $_ammenities comes from another query. I store multiple id's in 1 field separating them with a space //then I use explode on that string to create my $_ammenities array, I then use this if statement to // determine if the id in my query above is in the $_ammenities array //if it is, then I echo a checked="checked" in there, if it is not, then I don't. echo '<label><input type="checkbox" name="ammenities[]" value="'.$row->ammenityid.'" id="ammenities[]" checked="checked">'.$row->ammenity.'</label><br>'; } else { echo '<label><input type="checkbox" name="ammenities[]" value="'.$row->ammenityid.'" id="ammenities[]">'.$row->ammenity.'</label><br>'; } } ?> Not a real complex piece of code, so I hope it helps ya. Nate Quote Link to comment https://forums.phpfreaks.com/topic/69352-checkboxes/#findComment-348462 Share on other sites More sharing options...
phat_hip_prog Posted September 14, 2007 Share Posted September 14, 2007 I have a wrapper for virtually every form element, and again for fitting into my standard table! LOL Quote Link to comment https://forums.phpfreaks.com/topic/69352-checkboxes/#findComment-348472 Share on other sites More sharing options...
chronister Posted September 14, 2007 Share Posted September 14, 2007 I have a wrapper for virtually every form element, and again for fitting into my standard table! LOL I would be interested in seeing how you do that. I am working on redoing my companies .com site and we got some pretty lengthy forms e.g. Employment App, Franchise App, Contact forms etc. These freaking things get pretty tedious especially when the forms are over 20 fields. Hell, I was happy that I finally realized there is an easier way to assign friendly names to the field $_POST vars foreach($_POST as $k=>$v) { echo '$'.$k.'=trim($_POST[\''.$k.'\']);<br>'; } For a field called fname it will return $fname=trim($_POST['fname']); Makes it a lot easier to get 30-40 fields all named properly and such. Good stuff though, I may be trying to come up with some things like that to make development easier. Nate Quote Link to comment https://forums.phpfreaks.com/topic/69352-checkboxes/#findComment-348479 Share on other sites More sharing options...
phat_hip_prog Posted September 14, 2007 Share Posted September 14, 2007 I also stick in injection checking at the same point as you do that... Quote Link to comment https://forums.phpfreaks.com/topic/69352-checkboxes/#findComment-348482 Share on other sites More sharing options...
chronister Posted September 14, 2007 Share Posted September 14, 2007 I develop on about 3 different pc's. work desktop, work laptop, home desktop.... I believe in at least one of em I got the injection piece in there. The ones I am working with now are not inserted in the DB, they are just emailed so I am not concerned about injection. Nate Quote Link to comment https://forums.phpfreaks.com/topic/69352-checkboxes/#findComment-348483 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.