malatamil 9 Junior Poster

I have one dropdownlist in that i want to display some categories. and second is subcategory in checkboxes ,while selecting any category from dropdownlist that related subcategory should display in checkbox, after that whatever i need to select in checkbox values to store in database.
here is my code:

Main Category:

           <select name="catagory" id="catagory">
            <option value='' selected="selected">...Select a Category...</option>
            <?php  
                include("db.php");
                $query = "SELECT `id`,`MainCatagory` FROM `catagorykeywords` ";
                $result = mysql_query($query);

    //echo mysql_error();
    while($rows = mysql_fetch_array($result))
    {
    $selected = "";
    $categoryId = $rows['id'];
    $categoryName = $rows['MainCatagory'];
    if($catid == $categoryId)
    {
    $selected = "selected";
    }
    ?>
            <option value="<?php echo $categoryId; ?>" <?php echo $selected; ?> id="dosecondfilter" onchange="dosecond()"><?php echo $categoryName; ?></option>
            <?php }  ?>

         </select>


<div class="subcat_tick" id="subcat_tick"><ul>                
            <?php  

                include("db.php");
                $getval = 
                $query = "SELECT DISTINCT `Subcatagory`,`id`, `MainCatid` FROM `subcatagorykeywords` ";
                $result = mysql_query($query);

    //echo mysql_error();
    while($rows = mysql_fetch_array($result))
    {
    $selected = "";
    $categoryId = $rows['id'];
    $categoryName = $rows['Subcatagory'];
    if($catid == $categoryId)
    {
    $selected = "selected";
    }
    ?>
    <li><input name="relatedcatagories" class="tick" type="checkbox" value="<?php echo $categoryId; ?>" /><span><?php echo $categoryName; ?></span></li>
    <?php } ?></ul>
         </div>

Thanks in advance..

Dani AI

Generated

A few focused corrections and a simple, robust approach.

The immediate problems in 's code are: the onchange belongs on the <select> element (not on each <option>), the subcategory SQL must filter by the selected main category (WHERE MainCatid = ?), checkbox inputs need a name that accepts multiple values (for example name="subcats[]"), and the old mysql_* functions are deprecated — use PDO or mysqli with prepared statements. See the onchange behavior and event docs on MDN for details (onchange docs) and the PDO prepared-statement guide (PDO prepared statements).

Two implementation options:

  • Server-side reload: put the onchange on the select and either submit the form or reload the page with ?mainId=.... The server query becomes SELECT id, Subcatagory FROM subcatagorykeywords WHERE MainCatid = :mainId and then render checkboxes whose name is an array. Validate and cast incoming IDs to integers before using them.

  • Client-side (AJAX): call an endpoint that returns JSON for the selected main category and rebuild the checkbox list in the DOM. Using fetch() is simple and progressive — see MDN for examples (Using Fetch).

Storing selected checkboxes: accept $_POST['subcats'] (an array), validate elements (cast to int), and insert using prepared statements inside a transaction. Follow safe DB practices to avoid SQL injection (see OWASP guidance: SQL Injection).

Common pitfalls: ensure option values are numeric IDs, handle the empty-array case, add server-side validation even when using AJAX, and include CSRF protection if the form changes data.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.