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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:40:31+00:00 2026-06-10T07:40:31+00:00

I have a dropdown list(in a php file and without form tag) print <select

  • 0

I have a dropdown list(in a php file and without form tag)

print "<select id="animal" onchange="getSelectedItem(this.value)">
<option value = "Dog"> Dog </option>
<option value = "Cat"> Cat </option>
</select>";

and a variable $selAnimal

and a javascript function

function getSelectedItem(opt){
    //$selAnimal = opt <- how do i do this?
}

As much as possible I wouldn’t like the page to reload so I avoid putting this inside a form and submitting as I select. I can’t get to make $.post method work. I know for a fact that this cannot be directly done since php is server side and javascript is client side.
I also thought of putting the selected value inside a hidden component(span or something) and get the value from it. But I have no idea how to get it from the component. I’ve also seen use of AJAX or jquery but I’m not really knowledgeable enough.

I need to save this value to the php variable since I’ll be using it as basis for the options in my second dropdown. For example, if I choose Dog, the dropdown list would have dog breeds as options.

I’ve spent days looking for possible solutions everywhere. Help would be very much appreciated. Thank you!

  • 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-10T07:40:33+00:00Added an answer on June 10, 2026 at 7:40 am

    here is the solution

    <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
        $('#animal').change(function(){
        $.post('loadbreeds.php', { animal:$('#animal').val() },
            function(data){
                $('#breedList').html(data);
            });
        });
    });
    </script>
    
    <form method="post" >
        <div id="animalList">
            <select id="animal" name="animal">
                <option value="">--Select--</option>
                <?php if (!empty($animals)) { foreach ($animals as $value) { ?>
                    <option value="<?php echo $value['animalKey']; ?>"><?php echo $value['animalName']; ?></option>
                <?php }} ?>
            </select>
        </div>
        <div id="breedList">
            <select id="breed" name="breed">
                <option value="">--Select--</option>
            </select>
        </div>
        <input type="submit" value="Submit" />
    </form>
    

    code for loadbreeds.php

    $animalKey = $_POST['animal'];
    $breeds = selectByKey($animalKey); // code for selecting breeds
    
    <select id="breed" name="breed">
        <option value="">--Select--</option>
        <?php if (!empty($breeds)) { foreach ($breeds as $value) { ?>
            <option value="<?php echo $value['breedKey']; ?>"><?php echo $value['breedName']; ?></option>
        <?php }} ?>
    </select>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the dropdown list like this <?php echo $form->dropdownList($customers,'customer_name', CHtml::listData(Customers::model()->findAll(), 'id', 'customer_name'), array(
I have a dropdown list. I am using onchange event of this dropdown to
i have two dropdown list. first drop down:1 enter code here <form:select path=custName id=custName>
Below is an old PHP function I have for a dropdown list for users
I have a php page with a dropdown list that is populated by a
I have a dropdown list and a select list. Both contain quite a few
Basically, I have populated a dropdown list using php from a database on a
I have a small PHP script that takes the values from a dropdown list
I have a simple form that has a list (dropdown list generated from a
I have a dropdown list where I'd like to use the value of the

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.