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 7728219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:39:48+00:00 2026-06-01T05:39:48+00:00

Sorry in advance everyone for this question as I know the cascading select boxes

  • 0

Sorry in advance everyone for this question as I know the cascading select boxes has been done to death but I can’t seem to find any good help. I’ve tried various things but it all seems to fail and I’m not understanding why.

Here’s the jquery I have currently:

function tester() {
$("select#type").attr('disabled', 'disabled');
$("select#cat").change(function(){
            var vid = $("select#cat option:selected").attr('value');
            var request = $.ajax({
                        url: "show_type.php",
                        type: "POST",
                        data: {id : vid}
                      });
                request.done(function(msg) {
                  $("#result").html( msg );
                });
                request.fail(function(jqXHR, textStatus) {
                  alert( "Request failed: " + textStatus );
                });
        });
}

Don’t mind the first section of the code with the select#type and select#cat as these are for what I was trying to get the code to populate at first, however the .change is my trigger for the .ajax request. The rest of the code I’m merely trying to dump a simple return message into an empty div#result upon a successful ajax request.

I ran a test, and the var vid populates correctly.

Here’s the simple PHP file I’m trying to call with the ajax:

<?php
$requ;

if (isset($_POST['id'])) {
   $requ = 'Worked';
} else {
   $requ = "didn't work";
}


echo $requ;
?>

I thought perhaps the problem was the id wasn’t being passed properly so I altered the PHP script to give me any valid output regardless of whether the $_POST was set or not.

I won’t post the HTML as I’m just trying to dump this all into a div while I test it. When I run the script I get the ‘Request Failed’ error message with a message of “error”.

Here is the other jquery & PHP I have also tried, using the .post instead of the .ajax:

function tester() {
$("select#type").attr('disabled', 'disabled');
$("select#cat").change(function(){

            $("select#type").html("<option>wait...</option>");

            var vid = $("select#cat option:selected").attr('value');


            $.post("show_type.php", {id:vid}, function(data){
                $("#result").empty().append(data);
            }, "json");
      });
}

And the PHP to accompany this particular jquery:

$requ = $_POST['id'];

$ret = 'You selected: ' . $requ;

echo json_encode($ret);

Again, it all failed. I also tried the above code without using the json encoding/parameters. All I want to do is a simple (so I would think) cascading select dropboxes. The second box to be dependent of the first boxes selection. I’m beginning to think that this all just may not be worth it and just sticking strictly to PHP with links to resubmit the page with a GET and populate a new section or div with the results of the first click. Any help or suggestions you might have would be greatly appreciated, I’ve spent 2 solid days trying to figure this all out. Thanks in advance

  • 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-01T05:39:49+00:00Added an answer on June 1, 2026 at 5:39 am

    Alright, I got it fixed. Thanks to Mian_Khurram_ljaz for making me take a different look at the hierarchical structure of the file. I was assuming that since the js was calling the php file, by placing the php file in the same folder as the js, I could call the php by using the url: show_type.php but that was actually wrong. The structure is considered from the actual page invoking the js and php, and therefore the url should have been js/show_type.php since I had the show_type.php file in my js folder.

    It’s always the little mistakes that take you days to figure. For those in the future looking to find decent code for cascading select drop boxes, here is my functioning and fully expanded code (which also includes a tri-level cascade)
    jQuery:

    function project() {
    $("select#model2").attr('disabled', 'disabled');
    $("select#brand2").attr('disabled', 'disabled');
    $("select#project").change(function(){
       $("select#model2").attr('disabled', 'disabled'); // if changed after last element has been selected, will reset last boxes choice to default
       $("select#model2").html('<option selected="selected">Choose...</option>'); 
       $("select#brand2").html("<option>Please wait...</option>");
       var pid = $("select#project option:selected").attr('value');
    
       $.post("handler/project.php", {id:pid}, function(data){
           $("select#brand2").removeAttr("disabled");
           $("select#brand2").html(data);
       });
    });
    $("select#brand2").change(function(){
       $("select#model2").html("<option>Please wait...</option>");
       var bid = $("select#brand2 option:selected").attr('value');
       var pid = $("select#project option:selected").attr('value');
    
       $.post("handler/projBrand.php", {proj: pid, bran: bid}, function(data){
           $("select#model2").removeAttr("disabled");
           $("select#model2").html(data);
       });
      });
    }
    

    Just call the function in the $(document).ready of your js.

    Notice the comment, having this ‘redundant’ call to disable and force the last box to select the default is just in case the user makes a selection in all 3 boxes but goes back to the first box and changes the selection.

    Here is the php handler file:

    <?php
    include_once('../includes/file.inc');
    
    
    $request = $opt -> getModelvBrand();
    
    echo $request;
    ?>
    

    The other handler file for the jQuery is nearly exactly the same, only invoking a different method in the class file.

    And lastly, the HTML:

    <form action="" method="post">
           <select id="project">
              <option value="0">Choose...</option>
              <?php echo $opt -> getProject();?> //populates first box on page load
           </select>
           <select id="brand2">
              <option value="0">Choose...</option>
           </select>
           <select id="model2">
              <option value="0">Choose...</option>
           </select>
           <br /><br />
           <input class="like-button" type="submit" title="Submit" value="" />
    </form>
    

    Thanks again Mian for making me take a different look at my file(s).

    Hope this code helps someone else in the near future.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry in advance as this question is similar (but not the same!) to others.
Sorry in advance if this has been answered, I've searched repeatedly here and the
In advance: sorry for the noob question but I'm learning Cocoa & Objective-C and
Sorry if this is too simple, but thanks in advance for helping. This is
Sorry for this not being a real question, but Sometime back i remember seeing
Sorry in advance if I'm not explaining this correctly. I want to know if
sorry in advance I have searched the whole site but I can not find
Sill Q I know - sorry in advance - but how do you read
I'm sorry in advance if this question is flawed. I'm pretty new to databases(I
Sorry in advance if this is a repeat question. I didn't see it listed

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.