envexlabs Posted September 25, 2007 Share Posted September 25, 2007 Hey, I have a function that renders out tag categories, and then all the tags within said category. Each tag has a checkbox beside it, i am wondering if it's possible to only allow a user to check 4 tags. Here is the function: $select_tags_query = mysql_query('SELECT * FROM `tag_category`'); while($tag_category = mysql_fetch_row($select_tags_query)) { echo '<div class="tag_cat">'; //renders out the category name echo '<h2>' . $tag_category[1] . '</h2>'; $tags_query = mysql_query('SELECT * FROM `tags` WHERE `cat_id` = ' . $tag_category[0] . ''); //renders out each tag in the category while($tags = mysql_fetch_row($tags_query)) { echo '<p><input type="checkbox" />' . $tags[1] . '</p>'; } echo '</div> <!-- tag_cat div -->'; } This is what is outputs: Quote Link to comment Share on other sites More sharing options...
peeps Posted September 25, 2007 Share Posted September 25, 2007 I am not to familiar with Javascript but you would have to use the onclick method and validate the number of boxes checked. I hope that points you in the right direction. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted September 25, 2007 Share Posted September 25, 2007 definitely a JS thing, however what you can do is make it an array <input type="checkbox" name="Value[]" /> then on page 2 simply say <?php if(count($_POST['Value']) > $desired count){ //Error it } ?> Quote Link to comment Share on other sites More sharing options...
envexlabs Posted September 25, 2007 Author Share Posted September 25, 2007 Thanks for the suggestion, i would want it to be a real time thing though, will look into the JS! Thanks again Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted September 25, 2007 Share Posted September 25, 2007 always good if its crucial to do both as JS can be disabled. Quote Link to comment Share on other sites More sharing options...
envexlabs Posted September 26, 2007 Author Share Posted September 26, 2007 Hey, I have encountered another problem. When I post the form, this is what i get when i print_r($_POST): Array ( [tag20] => on [tag81] => on [change_tags] => true ) How can I figure out, out of 200 tags, which 2 have been chosen, because the $_POST[tagx] will change depending on which tags are chosen?! Thanks, envex Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted September 26, 2007 Share Posted September 26, 2007 the trick to this is Make creation and processing one in the same <?php $fields = array( "Mens Apparel" => array(), "Womans Apparel" => array() ); $i = 0; foreach($fields as $key => $value){ echo "<b>".$key."<b/>"; foreach($value as $value2){ echo "<input type=\"checkbox\" name=\"fields[".$i."]" />"; $i++; } <br/> }?> then they have a counting system in the multi d array and you can pull it off that. Quote Link to comment Share on other sites More sharing options...
envexlabs Posted September 26, 2007 Author Share Posted September 26, 2007 I will try to make sense of this Thanks again. Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 26, 2007 Share Posted September 26, 2007 I don't see anywhere in the code that the value attribute is being set. The value will tell you which one was chosen! Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted September 26, 2007 Share Posted September 26, 2007 that is an option, however checkboxes on older browsers do not send Value in the POST request as they where assumed to be boolean type so if it was checked it send if not that form input was not sent. What actually might work for you is a multi select. Quote Link to comment Share on other sites More sharing options...
envexlabs Posted September 26, 2007 Author Share Posted September 26, 2007 Hey, I've figured it out: if($_POST[submit] == "true"){ $tag = $_POST['tag']; foreach ($tag as $tagid) { //inserts the chosen tags into the database $insert_tags_query = mysql_query('INSERT INTO `store_tags` (`store_id`, `tag_id`, `order_id`) VALUES (\'' . $au[member_id] . '\',\'' . $tagid . '\',\'0\')'); } }else{ //do nothing } I am, however, back to my original problem. Javascript wont work because each checkbox doesn't have a unique ID. Anyone know a work around to this? Thanks, envex 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.