MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

How to show City List as per Country and State select in DropDown using AJAX in Laravel? Part-2

How to use AJAX in DropDown in Laravel? Example as in Country, State, and City.

To Click Part-1 of its first blog. Now, we’ll start the next part of this.

Inserting data into Country, State and City tables

Step 15. We can insert data into tables through CRUD functions or directly by Database. Here, we go direct to insert data into tables. Go to http://localhost/phpmyadmin -> Click your created database country_state_city ->Click countries table ->Click Insert.

PhpMyAdmin Insert tab for the countries table in the country_state_city database

Step 16. As well, we have to insert data into states and cities tables.

PhpMyAdmin states table with eight states such as Jharkhand, Punjab and New York linked by country_id
PhpMyAdmin cities table with six cities such as Bokaro, Ranchi and Lahore linked by state_id

Step 17. Go to resources/views/welcome.blade.php file and give a link. Laravel provides a helper function, asset(), which generates a URL to application assets(CSS/JavaScript/images files).

{{asset('css/app.css')}}  Code language: JavaScript (javascript)
Welcome.blade.php in Sublime Text with stylesheet link using asset('css/app.css') highlighted

Step 18. As well, we have to generate url js file by asset() function. Go to Download the compressed, production jQuery 3.5.1 right-click the link, copy all and paste within public/js folder->create jquery.js file.

<script src="{{asset('js/jquery.js')}}"></script>Code language: HTML, XML (xml)

Step 19. Write logic of DropDown selection using AJAX for Country within welcome.blade.php file

<script>
           $(document).ready(function(){
            $('select[name="country"]').on('change',function(){
                var country_id= $(this).val();
                if (country_id) {
                 $.ajax({
                    url: "{{url('/getStates/')}}/"+country_id,
                  type: "GET",
                  dataType: "json",
                  success: function(data){
                    console.log(data);
                    $('select[name="state"]').empty();
                    $.each(data,function(key,value){
                        $('select[name="state"]').append('<option value="'+key+'">'+value+'</option>');

                    });
                  }
                 });
                }else {
                     $('select[name="state"]').empty();
               }
           });
             
           });
       </script>Code language: HTML, XML (xml)

Step 20. Similarly, write logic of DropDown selection using AJAX for State.

Here’s the full code.

Laravel DropDown Feature page with empty Country, State and City select boxes
Ex:- DropDown

Whenever we select the name of country so automatically the name of the states’ list will have appeared. As well as, Cities’ list will be appeared on clicking the name of the state.

Laravel dependent dropdowns showing India, Jharkhand and Bokaro selected

Thanks

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
With MotoShare.in, you can book a bike instantly, enjoy doorstep delivery, and ride without worries. Perfect for travelers, professionals, and adventure enthusiasts looking for a seamless mobility solution.

Related Posts

What is Jquery and use cases of Jquery?

What is Jquery? Jquery is a fast and concise JavaScript library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. It…

Read More

Laravel – select dropdowns that filter results in a table ‘on change’

Hello, I am working on an application that is kind of an eCommerce application that have a table of product and I want to add a filter…

Read More

How to Image Upload and Crop in Laravel with Ajax

In this tutorial, I am going to tell you how to image upload and crop in Laravel with Ajax. To crop the image using jQuery croppie plugin,…

Read More

Image Upload and Crop using PHP and jQuery

In this PHP tutorial, I am going to tell you how to crop an image using jQuery and upload the image via Ajax. Using the Croppie plugin…

Read More

How to do Auto Load and Refresh Div every Seconds with jQuery and Ajax with PHP Script.

In this tutorial we are going to learn how to do auto load and refresh div every second by using JQuery and Ajax method with PHP Script….

Read More

Searching, Column Sorting with Pagination using Ajax in Laravel 5.5

Step 1 : Instal Laravel Application  If you have not install Laravel application, then you have to open command prompt, and run composert command, and then after…

Read More