Jump to content

Recommended Posts

I hope that this is in the right forum.

 

I am in the process of writing a block of code that will allow the use of chained select boxes. I am pretty sure that it is a AJAX issue. When I select from the first select box. The second select box does not fill with anything. It gives me an empty select box.

 

Here is the code that I am using. Anyone have any suggestions on what to do?

<?php
$host = 'localhost';
$username = 'root';
$dbname = 'classified';
$dbpassword = '';

$connection = mysql_connect($host, $username, $dbpassword);
$db = mysql_select_db($dbname, $connection);

if (!$connection) {
    die('Could not make a connection to MySql Database' . mysql_error());
}

if (!$db) {
    die('Could not connect to DB $dbname' . mysql_error());
}

function showCategory() {
    $select_sql = "SELECT * FROM category ORDER BY cat_id";
    $cat_select = mysql_query($select_sql);
    $category = "<option value='0'>Select a Category......</option>";
    while ($row = mysql_fetch_array($cat_select)) {
        $category .= "<option value='" . $row['cat_id'] . "'>" . $row['category'] . "</option>\n  ";
    }
    return $category;
}

function subCategory() {
    $sub_sql = "Select * From sub_cat where cat_id='" . $_POST['id'] . "'";
    $sub_select = mysql_query($sub_sql);
    $sub = "<option value='0'>Select a Sub Category</option>";
    while ($row = mysql_fetch_array($sub_select)) {
        $sub .= "<option value='" . $row['sub_cat_id'] . "'>" . $row['sub_cat'] . "</option>\n  ";
    }
    return $sub;
    echo $_POST['id'];
}
?>
<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                $("select#sub_category").attr("disabled","disabled");
                $("select#category").change(function(){
                    $("select#sub_category").attr("disabled","disabled");
                    $("select#sub_category").html("<option>wait...</option>");
                    var id = $("select#category option:selected").attr('value');
                    $.post("<?php echo $_SERVER['PHP_SELF']; ?>", {id:id}, function(data){
                        $("select#sub_category").removeAttr("disabled");
                        $("select#sub_category").html(data);
                    });
                });
            });
        </script>
    </head>
    <body>
        <br/><form action="" method="post" name="cars">
            <select id="category" name="category">
                <?php
                $select_sql = "SELECT * FROM category ORDER BY cat_id";
                $cat_select = mysql_query($select_sql);
                $category = "<option value='0'>Category</option>";
                while ($row = mysql_fetch_array($cat_select)) {
                    $category .= "<option value='" . $row['cat_id'] . "'>" . $row['category'] . "</option>\n  ";
                }

                echo $category;
                ?>
            </select>
            <select id="sub_category" name="sub_category">
                <?php
                $sub_sql = "Select * From sub_cat where cat_id='" . $_POST['id'] . "'";
                $sub_select = mysql_query($sub_sql);
                $sub = "<option value='0'>Sub Category</option>";
                while ($row = mysql_fetch_array($sub_select)) {
                    $sub .= "<option value='" . $row['sub_cat_id'] . "'>" . $row['sub_cat'] . "</option>\n  ";
                }

                echo $sub;

                echo $sub_sql;
                ?>
            </select>
            <input type="submit" name="fire" value="GO"/>
        </form>

    </body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/259830-submitting-an-id-through-post/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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