PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 24, 2022

[FIXED] Ajax laravel dropdown not returning the id of selected value

 January 24, 2022     ajax, laravel, laravel-5     No comments   

Issue

I am passing the hotel id as a parameter to my function to get all the rooms in that hotel. But it is returning an error. I want to return in a JSON format the id of the hotel. this is my javascript function

function showRooms($hotel_plan_id) {
   var id = $hotel_plan_id; 
 if (id !== "") {
var token = $('meta[name="_token"]').attr('content');
     $.ajax({
    url: '/rooms'+id,
    type: "Get",
    dataType: 'JSON',
    data: "{id:" + JSON.stringify(id) + ", _token:" + token + "}",
success:function(data)
    {
      alert('xdata:' + data);
    //$('#'+dependent).html(result);
    },
    error: function (result) {
      alert("Error occur in the rooms()");
    }
 });

this is my controller

public function rooms(id $id){

    $response = array(); 
    $response =$id;

    return response()->json($response);
}

this is my route

Route::get('/rooms', 'HomeController@rooms')->name('/rooms');

Solution

your URL url: '/rooms'+id, and Route::get('/rooms', 'HomeController@rooms')->name('/rooms'); not valid, please see my code i am edited your code

   //Change your js function
   function showRooms(hotel_plan_id) {
     var id = hotel_plan_id; 
      if (id !== "") {
        $.ajax({
          type: "GET",
          dataType: 'JSON',
          url:'{{ route('rooms', '') }}/'+id,
          data:{_token:'{{ csrf_token() }}'},
          success:function(data){
            alert('xdata:' + data);
            //$('#'+dependent).html(result);
          },
          error: function (result) {
            alert("Error occur in the rooms()");
          }
        });
      }
    }

    //Change your function 
    public function rooms($id){
      return response()->json(['id'=>$id]);
    }

    //Change your route
    Route::get('rooms/{id}', 'HomeController@rooms')->name('rooms');


Answered By - Md Abdul Awal
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing