Hello Friends i ahve a drop down in which all the values are coming from the database now i have used ajax and jquery for getting the sub categories in another dropdown and its working fine but the problem is when i am using the name of the select box for insertion it always stores the parent id of the category........Please help me friends i am almost lost my job :-(

Recommended Answers

All 8 Replies

Hello Friends i ahve a drop down in which all the values are coming from the database now i have used ajax and jquery for getting the sub categories in another dropdown and its working fine but the problem is when i am using the name of the select box for insertion it always stores the parent id of the category........Please help me friends i am almost lost my job :-(

With the information you are giving all I can say is something is wrong.

Post the relevant code so we can have a look at it.Thanks.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title></title>

<script type="text/javascript" src="jquery-1.3.2.js"></script>
<script type="text/javascript" src="jquery.livequery.js"></script>
<script type="text/javascript">

$(document).ready(function() {

    //$('#loader').hide();

    $('.parent').livequery('change', function() {

        $(this).nextAll('.parent').remove();
        $(this).nextAll('label').remove();

        $('#show_sub_categories').append('<img src="loader.gif" style="float:left; margin-top:7px;" id="loader" alt="" />');

        $.post("get_chid_categories.php", {
            parent_id: $(this).val(),
        }, function(response){

            setTimeout("finishAjax('show_sub_categories', '"+escape(response)+"')", 400);
        });


        return false;
    });
});

function finishAjax(PID, response){
  $('#loader').remove();

  $('#'+PID).append(unescape(response));
} 

function alert_id()
{
    if($('#sub_category').val() == '')
    alert('Please select a category.');
    else
    alert($('#sub_category').val());
    return false;
}

</script>
<style>
.both h4{ font-family:Arial, Helvetica, sans-serif; margin:0px; font-size:14px;}
#search_category_id{ padding:3px; width:200px;}

.parent{ padding:3px; width:150px; float:left; margin-right:12px;}
.both{ float:left; margin:0 0px 0 0; padding:0px;}
</style>
</head>
<?php
        include('config/databse_config.php');?>
<body>
<form action="" method="get" onsubmit="return alert_id();">
<div style="padding-left:30px; height:710px;">

    <br clear="all" /><br clear="all" />

    <div id="show_sub_categories">
        <select name="search_category" class="parent">
        <option value="" selected="selected">-- Categories --</option>
        <?php
        $query = "SELECT CategoryID, Name, Discount_percent, Discount_flat, Description, PID 
FROM slenrjrt_shopping.Category where PID = 0";
        $results = mysql_query($query);

        while ($rows = mysql_fetch_assoc(@$results))
        {?>
            <option value="<?php echo $rows['CategoryID'];?>"><?php echo $rows['Name'];?></option>
        <?php
        }?>
        </select>    

    </div>
    <div><input type="submit" name="sub" /></div>
    <br clear="all" /><br clear="all" />

</div>
</form>
</body>
</html>

and my code for getting the child category is here get_chid_categories.php

<?php

include('config/databse_config.php');

if($_REQUEST)
{
    $id     = $_REQUEST['parent_id'];

    $query = "SELECT CategoryID, Name, Discount_percent, Discount_flat, Description, PID 
FROM slenrjrt_shopping.Category where PID = ".$id;
    $results  = @mysql_query( $query);
    $num_rows = @mysql_num_rows($results);
    if($num_rows > 0)
    {?>
        <select name="sub_category" class="parent" id="sub_category">
        <option value="" selected="selected">-- Sub Category --</option>
        <?php
        while ($rows = mysql_fetch_assoc(@$results))
        {?>
            <option value="<?php echo $rows['CategoryID'];?>"><?php echo $rows['Name'];?></option>
        <?php
        }?>
        </select>
        <input type="hidden" name="subcat" value="<?php echo $_REQUEST['sub_category']; ?> " />
    <?php    
    }
    else{echo '<label style="padding:7px;float:left; font-size:12px;">No Record Found !</label>';}
}
?>

i am not getting my the subcategory id

try:

$query = "SELECT CategoryID, Name, Discount_percent, Discount_flat, Description, PID
FROM slenrjrt_shopping.Category where PID = '$id'";

Thanks gamebits for reply but the problem is that i can not select the value of 2nd drop down because i have to store the category id again in the database.

Please friends give me some solution of my problem

Can Anyone help me in my code

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.