🎓 A1Learner
🔍
0 of 50 lessons completed

Laravel HTML Forms

A form is an HTML element that collects input and sends it to the server. A form sends its fields to a route when the reader submits it.

Open a form

The form sends its fields to the hello route.

<form method="POST" action="/l24/hello">

Add a field and a button

The name attribute labels the field for the route.

    <input type="text" name="name">
    <button type="submit">Say hello</button>

Read the field

The route reads the field by its name.

return 'Hello, ' . $request->input('name');

The POST route

The route runs when the form is submitted.

Route::post('/l24/hello', function (Request $request) {
    return 'Hello, ' . $request->input('name');
});

Example

<form method="POST" action="/l24/hello">
    <input type="text" name="name">
    <button type="submit">Say hello</button>
</form>

Route::post('/l24/hello', function (Request $request) {
    return 'Hello, ' . $request->input('name');
});

Key points

  • Use the method attribute to set how the form sends its data.
  • Use the action attribute to set which route receives the data.
  • Use an input element to add one field to a form.
  • Use the name attribute so the route can read each field.

In this example, the form sends a field named name to /l24/hello. The route reads the field with input(). It returns a greeting with the given name. A form is an HTML element that collects input and sends it to the server.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which attribute sets the route that receives a form?