Auto Select Dynamic Dropdown Matching value of php variable

Auto Select Dynamic Dropdown Matching value of php variable

i want to automatically select value of dropdown matching value of php variable

i want to automatically select value of dropdown matching value of php variable

here is my

    <select class="destinationId" id="destination" name="destinationId" onchange="getNationalityAndLiving(this.value,'','');">
                  <option value="" disabled selected>Select Traveling to</option>
                                <option data-prefix="azerbaijan" value="azerbaijan" >
                  Azerbaijan              </option>
                                <option data-prefix="cambodia" value="cambodia" >
                  Cambodia              </option>
                                <option data-prefix="egypt" value="egypt" >
                  Egypt              </option>
                                <option data-prefix="india" value="india" >
                  India              </option>
                                <option data-prefix="kenya" value="kenya" >
                  Kenya              </option>
                              </select>


<select class="nationality" name="nationality" id="nationality"> <option value="" disabled selected>Select Citizen of</option> </select>

this is my ajax code to retrive dynamic dropdown for select select option "nationality"

    <script>
$(document).ready(function(){

$("#destination").change(function(){
    var deptid = $(this).val();
    var dbnm = "country";
    var res = dbnm.concat(deptid);
    $('.overlay').show();
    $.ajax({
        url: 'getcountry.php',
        type: 'post',
        data: {dest:res},
        dataType: 'json',
        success:function(response){
            var len = response.length;

            $("#nationality").empty();
            for( var i = 0; i&lt;len; i++){
                var id = response[i]['id'];
                var name = response[i]['name'];

                $("#nationality").append("&lt;option data-nation='"+name+"' value='"+name+"'&gt;"+name+"&lt;/option&gt;");

            }
           $('.overlay').hide();            
        }
    });
});

}); </script>

i have some dynamic php variable coming on this page from previous url. for example

$nationality = "some value";

What i want is selected dropdown with value of $nationality in second dropdown,

if it was static i could have achieved this, but how to auto select in dynamic i am not able to get any workaround.

javascript php jquery

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

PHP json_decode Example | Decode JSON Data in PHP Tutorial

PHP json_decode is an inbuilt function that takes the JSON encoded string and converts it into a PHP variable. JSON stands for JavaScript Object Notation.

Hire PHP Developer

Looking to develop a PHP based website from scratch or revamp your existing website? **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")** has always been an industry leader for companies and business owners looking to hire...

Autocomplete Textbox with Javascript, PHP and MySQL

In this guide, we will walk through how to add autocomplete to a textbox, with the help of jQuery and also an alternative that works with just vanilla Javascript.

Understand and Use JavaScript's .forEach() vs. jQuery's .each()

There are two functions to deal with an array on the client-side – JavaScript's .forEach() and jQuery's .each(). Here, I will teach you how to use both of these methods with some sample code.

Hire Dedicated PHP Developer

Looking to hire affordable yet experienced PHP developers? **[Hire Dedicated PHP Developer](https://hourlydeveloper.io/hire-dedicated-php-developer/ "Hire Dedicated PHP Developer")**, who can convert your idea to reality, within the stipulated...