Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9055839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:03:10+00:00 2026-06-16T14:03:10+00:00

I’m stuck with this bit of code. By all accounts it should work, I’ve

  • 0

I’m stuck with this bit of code. By all accounts it should work, I’ve been staring at it for 2 hours now, I have no idea what is wrong.

I have two selections with options in divs. When one is selected with the right parent id, the sub category should appear, it does not for some reason though I feel I’ve done it all right – here is my html/php for the main page:

<li>
            <label for="Catid">Pick a Category:</label>
            <select id="Catid">
              <?php
                  $Products = New Products();
                  $Products->form_Cat_Picker();
                ?>
            </select>
          </li>
          <li>
            <label for="Subcatid">Sub Category:</label>
              <?php
                  $Products = New Products();
                  $Products->form_Subcat_Picker();
                ?>
          </li>
          <script type="text/javascript">
            $(document).ready(function() {
                $('#Catid').change(function(){
                    var optvalue = $(this).val(),
                    div = $('#' + 'parentid' + optvalue);
                    $('div').hide();
                    div.show();
                });
            });​
          </script>
          <li>

And here are the two functions:

function form_Cat_Picker() {
    $mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME);
    if (!$mysqli) {
        die('There was a problem connecting to the database.');
    }       
    $catPicker = "SELECT Catid, Catname
            FROM ProductCats
            ORDER BY Catid";
    if ($Result = $mysqli->query($catPicker)){
        if (!$Result) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        echo '<option value="">'.''.'</option>';
        while ($row = $Result->fetch_assoc()) {
            echo '<option value="'.$row["Catid"].'">'.$row["Catname"]."</option>";
            echo '</div>';
        }
    }
    $mysqli->close();
}
function form_Subcat_Picker() {
    $mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME);
    if (!$mysqli) {
        die('There was a problem connecting to the database.');
    }       
    $catPicker = "SELECT Subcatid, Subcatname, Parentid
            FROM ProductSubCats
            ORDER BY Subcatid";
    if ($Result = $mysqli->query($catPicker)){
        if (!$Result) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        $counter = 0;
        while ($row = $Result->fetch_assoc()) { 
            if ($counter >= 1) {
                if ($last_id != $row['Parentid']){
                    echo '</select>';
                    echo '</div>';
                }
            }
            if ($last_id != $row['Parentid']){
                    echo '<div id="parentid'.$row['Parentid'].'" style="display:none">';
                    echo '<select id="Subcatid">';
                }
            echo '<option value="'.$row["Subcatid"].'">'.$row["Subcatname"]."</option>";
            $last_id = $row['Parentid'];
            $counter++;
        }
        echo '</select>';
        echo '</div>';
    }
    $mysqli->close();
}

These output the following:

<li>
<label for="Catid">Pick a Category:</label>
<select id="Catid">
<option value=""/>
<option value="1">Sample 1</option>
<option value="2">Sample 2</option>
<option value="3">Sample 3</option>
<option value="4">Sample 4</option>
<option value="5">Sample 5</option>
<option value="6">Sample 6</option>
<option value="7">Sample 7</option>
</select>
</li>
<li>
<label for="Subcatid">Sub Category:</label>
<div id="parentid1" style="display: none;">
<select id="Subcatid">
<option value="1">Sub Sample 1</option>
<option value="2">Sub Sample 2</option>
</select>
</div>
</li>

So the output looks fine. The jquery seems correct.
But when I try to run it – the div id parentid1 does not appear when Sample 1 is selected.

Am I going blind or have I done something wrong? Thanks

EDIT

even works in jfiddle:
http://jsfiddle.net/VGDwm/
And I called in the jquery library properly with:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"/>

What could be wrong? Should the jquery be after or before the divs? Or is this an issue with calling in from php?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T14:03:11+00:00Added an answer on June 16, 2026 at 2:03 pm
    1. Don’t re-connect to the database every function
    2. Re-type the last line of the jquery, it has a hidden character at the end.

    so the line highlighted here:

          <script type="text/javascript">
            $(document).ready(function() {
                $('#Catid').change(function(){
                    var optvalue = $(this).val(),
                    div = $('#' + 'parentid' + optvalue);
                    $('div').hide();
                    div.show();
                });
            });​    <----- THIS LINE ------>
          </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have been unable to fix a problem with Java Unicode and encoding. The
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.