To handle AJAX requests and server-side updates in Laravel Livewire, you can utilize Livewire's built-in capabilities for handling form submissions and dynamic UI updates. Let's create an example where we submit a form asynchronously and update the UI based on the server response:
Successfull message
Step 1: Create a new Livewire component.
php artisan make:livewire CommentForm
Step 2: Implement the CommentForm logic.
// app/Http/Livewire/CommentForm.php
namespace App\Http\Livewire;
use Livewire\Component;
use App\Models\Comment;
class CommentForm extends Component
{
public $name;
public $email;
public $comment;
public function submitForm()
{
// Validation
$this->validate([
'name' => 'required',
'email' => 'required|email',
'comment' => 'required',
]);
// Save comment to database
Comment::create([
'name' => $this->name,
'email' => $this->email,
'comment' => $this->comment,
]);
// Clear form fields after successful submission
$this->reset(['name', 'email', 'comment']);
// Emit event for UI update
$this->emit('commentAdded');
}
public function render()
{
return view('livewire.comment-form');
}
}
Step 3: Create the Blade view.
<!-- resources/views/livewire/comment-form.blade.php -->
<div>
<form wire:submit.prevent="submitForm">
<input type="text" wire:model="name" placeholder="Name">
@error('name') <span class="error">{{ $message }}</span> @enderror
<input type="email" wire:model="email" placeholder="Email">
@error('email') <span class="error">{{ $message }}</span> @enderror
<textarea wire:model="comment" placeholder="Comment"></textarea>
@error('comment') <span class="error">{{ $message }}</span> @enderror
<button type="submit">Submit</button>
</form>
</div>
Step 4: Handle UI updates in the parent Blade view.
<!-- resources/views/welcome.blade.php -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Comment Form</title>
@livewireStyles
<style>
/* Style error messages */
.error {
color: red;
font-size: 12px;
}
</style>
</head>
<body>
<div>
<h1>Comment Form</h1>
@livewire('comment-form')
<!-- Show success message -->
<div wire:loading.remove wire:target="submitForm" wire:poll.500ms="commentAdded">
<p>Comment added successfully!</p>
</div>
<!-- Show loading spinner -->
<div wire:loading wire:target="submitForm">
<p>Loading...</p>
</div>
</div>
@livewireScripts
</body>
</html>
In this example, when the form is submitted, Livewire handles the AJAX request asynchronously, submits the form data to the server, validates it, and saves the comment to the database. After successful submission, it clears the form fields and emits a custom event (commentAdded). The parent Blade view listens for this event and updates the UI accordingly by showing a success message. Additionally, it shows a loading spinner while the form is being submitted. Livewire handles all the AJAX requests and UI updates seamlessly, simplifying form submissions and dynamic UI updates.
Top comments (0)