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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:05:01+00:00 2026-05-31T22:05:01+00:00

here is my situation. I have a select box that is populated from a

  • 0

here is my situation. I have a select box that is populated from a database SELECT command with PHP. The same select which is a select of countries has a jQuery event bind on it and on change it goes and load the flag of each country.

$("#CountryA").change(function () {
    var countryname = this.options[this.selectedIndex];
    var fileExist = $.ajax({
        url: './../theme/_images/flags/' + $.trim($(countryname).text()) + '.png',
        type: 'HEAD',
        error: function () {
            $("#flag").html('<img src= "./../theme/_images/flags/notFound.png" />');
        },
        success: function () {
            $("#flag").html('<img src= "./../theme/_images/flags/' + $.trim($(countryname).text()) + '.png" />');
        }
    });
})

and this is the PHP

<?php
while ($data = $connection->dbFetchArray($resultCountry)) {
    if ($data["Description"] == @$_GET['country'])
        echo "<option value='" . $data["Description"] . "' selected='selected'>" . $data["Description"] . "</option>";
    else
        echo "<option value='" . $data["Description"] . "'>" . $data["Description"] . "</option>";

}
?>

Also with php I managed to remember the selected value of the user before the sumbit. BUT after the user sumbits the form (I am using PHP_SELF so I remain in the same page) the flag dissapears.

Can I make the function to be executed again after the sumbit button? On pageload or something in order to have the flag of the selected country displayed to the user???

Thanks in advance!

======================EDIT===========================

Ok, thank you very much augustknight and everyone that took the time to respond… 😀

I made some minor changes to your code because these lines

    var country_name = $("#CountryA :selected").val();
    set_country_flag_image(this.options[this.selectedIndex]);

were giving me some weird HTMLOptionElement instead of the selected name. So here is the final code for future reference. 😀 THanks again!!!

$(function () {
    // Initialize the flag to the selected country (could also be done server-side)
    var country_name = $("#CountryA").val();
    set_country_flag_image(country_name);


    // Update the flag image when a country is selected
    $("#CountryA").change(function () {
        alert($("#CountryA").val());
        set_country_flag_image($("#CountryA").val());
    });
});

function set_country_flag_image(country_name) {
    $.ajax({
        url: './../theme/_images/flags/' + country_name + '.png',
        type: 'HEAD',
        error: function () {
            $("#flag").html('<img src= "./../theme/_images/flags/notFound.png" />');
        },
        success: function () {
            $("#flag").html('<img src= "./../theme/_images/flags/' + country_name + '.png" />');
        }
    });
}
  • 1 1 Answer
  • 1 View
  • 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-05-31T22:05:02+00:00Added an answer on May 31, 2026 at 10:05 pm

    I think this is what you are looking for.

    $(function () {
        // Initialize the flag to the selected country (could also be done server-side)
        var country_name = $("#CountryA :selected").val();
        set_country_flag_image( country_name );
    
        // Update the flag image when a country is selected
        $("#CountryA").change(function(){
            set_country_flag_image(this.options[this.selectedIndex]);
        });
    });
    
    
    function set_country_flag_image(country_name)
    {
        $.ajax({
            url:'./../theme/_images/flags/'+country_name+'.png',
            type:'HEAD',
            error:
                function(){
                    $("#flag").html('<img src= "./../theme/_images/flags/notFound.png" />'); 
                },
            success:
                function(){
                   $("#flag").html('<img src= "./../theme/_images/flags/'+country_name+'.png" />'); 
                }
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the situation I have a webpage which has one drop down called prefer.
Here's the situation I'm in: We have a field in our database that contains
Here's the situation: we have an Oracle database we need to connect to to
Here is my situation: I have one table that contains a list of drugs
here's the situation: I have a where in every cell all the area has
Here's the situation: I have a two images that are over 1024 in width
I have faced a very strange situation here. I am accessing database (MDB) through
I have a simple query like so: SELECT * FROM orders that returns all
Here is my situation, I have an ESRI Map Silverlight application that needs to
Here is the situation that I'm facing: I have two tables A and B.

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.