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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:01:18+00:00 2026-06-15T21:01:18+00:00

trying to create a dependant select function that fetch json value from database, but

  • 0

trying to create a dependant select function that fetch json value from database, but i can’t get to pass second value from select to get the last option, this is my code:

<?php
include("../../cons/cons.php");
$busco = "SELECT distinct(HostCountry) as country From schools WHERE 1 order by country asc";
$bus = mysql_query($busco) or die(mysql_error());
?>
<html>

<head>
<meta name="viewport" content="width=device-width, initial-scale=1"> 

 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
$(document).ready(function() {

//first, detect when initial DD changes
$("#states").change(function() {
    //get what they selected
    var selected = $("option:selected",this).val();
    //no matter what, clear the other DD
    //Tip taken from: http://stackoverflow.com/questions/47824/using-core-jquery-how-do-you-remove-all-the-options-of-a-select-box-then-add-on
    $("#cities").children().remove().end().append("<option value=\"\">Select a City</option>");
    //now load in new options if I picked a state
    if(selected == "") return;
    $.getJSON("getcities.php?method=getcities&returnformat=json",{"stateid":selected}, function(res,code) {
        var newoptions = "";
        for(var i=0; i<res.length; i++) {
            //In our result, ID is what we will use for the value, and NAME for the label
            newoptions += "<option value=\"" + res[i].VALOR + "\">" + res[i].NOMBRE + "</option>";
        }
        alert (newoptions);
        $("#cities").children().end().append(newoptions);
    });
});


// mi mano

$("#cities").change(function() {
    //get what they selected
    var selected2= $("select.cities").val();
    alert( selected2);
    //no matter what, clear the other DD
    //Tip taken from: http://stackoverflow.com/questions/47824/using-core-jquery-how-do-you-remove-all-the-options-of-a-select-box-then-add-on
    $("#subject").children().remove().end().append("<option value=\"\">Select a Subject</option>");
    //now load in new options if I picked a state
    if(selected2 == "") return;
    $.getJSON("getsubject.php?method=getsubject&returnformat=json",{"cityid":selected2}, function(res2,code) {
        var newoptions = "";
        for(var i2=0; i2<res2.length; i2++) {
            //In our result, ID is what we will use for the value, and NAME for the label
            newoptions2 += "<option value=\"" + res2[i2].VALOR + "\">" + res2[i2].NOMBRE + "</option>";
        }
        $("#subject").children().end().append(newoptions2);
    });
});
//hasta aca

})
</script>
</head>
<body>


<form method="post" action="">
<select name="states" id="states">
<option value="">Select Country</option>
<?php while ($b = mysql_fetch_array($bus)){
$dato = $b['country'];
$valor = str_replace(" ","_", $dato);
echo "<option value = '$valor'>$dato</option>";
}
?>
</select>


<select name="cities" id="cities">
<option value="">Select a City</option>
</select>  

<select name="subject" id="subject">
<option value="">Select a Subject</option>
</select>   

</form>

</body>
</html>

Can you give me a hint? i get undefined on the second alert….. Thanks

  • 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-15T21:01:19+00:00Added an answer on June 15, 2026 at 9:01 pm

    There is no select with class cities it has id cities, so use

    var selected2= $("#cities").val();
    

    or

    var selected2= $(this).val();
    

    instead of

    var selected2= $("select.cities").val();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
Trying to create a new Dedicated Cache Role in Windows Azure but get the
I'm trying to create a level 2 S-Function written in C that has different
I’m using VBA in Outlook 2010 and I’m trying to create a function that
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
Trying to create a black line in my view to separate text blocks but
Trying to create a macro which can be used for print debug messages when
Trying to create a generic video plugin for Expression Engine 2 where I can
I am trying to create a sidebar for a site that will allow a
I am trying to extract values from an XML file using linq to create

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.