Jump to content

only allow x number of items to be checked


envexlabs

Recommended Posts

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:

 

whas_example.jpg

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

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.

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.