John Davidson

php - Laravel changing view variable with ajax

0 comments
Message:


I am trying to make a script that when I click a flag button it will pick up the flag value "en" and replace it in my url and refresh the content without refreshing the page.


So far i have this :


                <li class="nav-item dropdown ml-lg-2">
<a class="nav-link px-0" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" data-offset="0,10">
<img alt="Português" style="height: 25px;" src="{{URL::asset('/img/flags/pt.svg')}}">
</a>
<div class="dropdown-menu dropdown-menu-sm" x-placement="bottom-start" style="position: absolute; will-change: transform; top: 0px; left: 0px; transform: translate3d(0px, 49px, 0px);">
<a href="{{route('user_register', 'pt')}}" class="dropdown-item"><img alt="Português" style="height: 25px;" src="{{URL::asset('/img/flags/pt.svg')}}">Português</a>
<a href="{{route('user_register', 'en')}}" class="dropdown-item"><img alt="Inglês" style="height: 25px;" src="{{URL::asset('/img/flags/us.svg')}}">Inglês</a>
</div>
</li>

But i wanted something like this :


$('.dropdown-menu a.dropdown-item.en').click((function(event){

const locale = 'en';

$.ajax({
url: RegisterUrl,
type:"GET",
data:{
locale,
},
success:function(response){
}
},
);
}));



I just dont know how to switch the locale variable.



My Answer:
and send it to the server using AJAX. The server will then update the view variable with the new flag value and return the updated view.

Here is an example of how you can achieve this in Laravel:

1. Create a route in your web.php file to handle the AJAX request:

php
Route::post('/update-flag', 'FlagController@updateFlag')->name('updateFlag');


2. Create a controller called FlagController with a method updateFlag:

php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FlagController extends Controller
{
public function updateFlag(Request $request)
{
$flag = $request->input('flag');

// Update the view variable with the new flag value
view()->share('flag', $flag);

// Return the updated view
return view('your-view');
}
}


3. Create a JavaScript file to handle the AJAX request:

javascript
$('.flag-button').click(function() {
var flag = $(this).data('flag');

$.ajax({
url: '{{ route('updateFlag') }}',
type: 'POST',
data: {
flag: flag
},
success: function(response) {
// Update the view with the updated content
$('#your-view-container').html(response);
}
});
});


4. In your view file, add the flag button and a container for the updated view:

html


{{-- Your view content here --}}



Now, when you click the flag button, it will send the flag value to the server using AJAX. The server will update the view variable with the new flag value and return the updated view, which will be displayed in the view container.

Rate this post

5 of 5 based on 4214 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us