0
0
Laravelframework~5 mins

Form input in Laravel

Choose your learning style9 modes available
Introduction

Form inputs let users send information to your website. Laravel helps you create and handle these inputs easily.

When you want users to register or log in.
When collecting feedback or contact details.
When users need to submit data like posts or comments.
When filtering or searching data on your site.
When updating user profiles or settings.
Syntax
Laravel
<?php
// In Blade template
<form method="POST" action="/submit">
    @csrf
    <input type="text" name="username" />
    <input type="submit" value="Send" />
</form>
Use @csrf to add a security token automatically.
The name attribute is important to identify the input data.
Examples
A simple text input for email with a placeholder.
Laravel
<input type="text" name="email" placeholder="Enter your email" />
A password input hides what the user types.
Laravel
<input type="password" name="password" />
A larger box for multi-line text input.
Laravel
<textarea name="message" rows="4" cols="50"></textarea>
A dropdown menu to pick one option.
Laravel
<select name="country">
  <option value="us">USA</option>
  <option value="ca">Canada</option>
</select>
Sample Program

This example shows a simple contact form. When the user submits their name and message, Laravel receives it and replies with a greeting message.

Laravel
<?php
// routes/web.php
use Illuminate\Http\Request;

Route::get('/contact', function () {
    return view('contact');
});

Route::post('/contact', function (Request $request) {
    $name = $request->input('name');
    return "Hello, $name! Your message was received.";
});

// resources/views/contact.blade.php
?><form method="POST" action="/contact">
    @csrf
    <label for="name">Name:</label>
    <input type="text" id="name" name="name" required />
    <br />
    <label for="message">Message:</label>
    <textarea id="message" name="message" required></textarea>
    <br />
    <input type="submit" value="Send" />
</form>
OutputSuccess
Important Notes

Always include @csrf in your forms to protect against attacks.

Use the name attribute to access input values in your controller or route.

Validate inputs on the server side to keep data safe and clean.

Summary

Form inputs let users send data to your Laravel app.

Use Blade templates with @csrf and name attributes.

Handle input data in routes or controllers using the Request object.