<h4>Laravel- Form</h4>
<p>So here let’s see how to make html form and submit its value to the controller. </p>
<p>- First HTML form is created in the view file.</p>
<p>- Then make a controller and you have to make two routes- one for the html form which is for the view and second for the controller and this will be the post route.</p>
<p>- After this all you can get value of html form to the controller.</p>
<p>So, first open the console and make a controller as-:</p>
<p>Php artisan make controller ‘users controller (controller name)’;</p>
<p>Now you find the controller file in code editor. Open the file and create a function inside it as-:</p>
<pre>
< ?php
Namespace App\Http\Controllers;
Use Illuminate\Http\Request;
Class UsersController extends Controller
{
Function getData()
{
Return “Form data will be here”;
}
}
</pre>
<p>Now let’s make a route, go to routes folder> open web.php file and write code as-:</p>
<pre>
Use Illuminate\Support\Facades\Route;
Use App\Http\Controllers\UsersController;
Route:: get (‘/’, function() {
Return view (‘welcome’);
});
Route::get (‘users’, [UsersController::class, ‘getData’]);
</pre>
<h5>- Create a HTML form and for that you need a html view.</h5>
<h5> Go to Views folder and create file “users.blade.php” and dimply write h1 tag and put some text and make a form as-:</h5>
<pre>
< h1> user login< /h1>
< form>
< input type=”text” name=”username” placeholder = “enter user id” /> < br>
< input type=”password” name=”userpassword” placeholder = “enter user password” /> < br>
< button type=”submit”>Login < /button>
< /form>
</pre>
<h5>- Now you need to define two more attributes on this form- one is action where it will be just submit so user section will be there for submit it. Method will be define as the method you are using like “post method”, it is the safest method. </h5>
<pre>
< h1> user login< /h1>
< form action = “users” method=”POST”>
< input type=”text” name=”username” placeholder = “enter user id” /> < br>
< input type=”password” name=”userpassword” placeholder = “enter user password” /> < br>
< button type=”submit”>Login < /button>
< /form>
</pre>
<p>Next you need to make a route for this page in web.php file. Code is written as-:</p>
<pre>
< ?php
Use Illuminate\Support\Facades\Route;
Use App\Http\Controllers\UsersController;
Route::get(‘/’, function() {
Return view(‘welcome’);
});
Route::post (“users” [UsersController::class, ‘getData’]);
Route::view(“login”, “users”);
)
</pre>
<p>Now to see the output go to browser and simply write- localhost:9000/login</p>
<div class=”img”>
<img src=”images/pic1.png” height=”300px” width=”500px”>
</div>
<p>Now add some id and password and whenever you will submit then it will give some error like page expired or something. Because you did not pass the csrf token so for this simply add in users.blade.php file as-:</p>
<h5>@csrf as-:</h5>
<pre>
< h1> user login< /h1>
< form action = “users” method=”POST”>
@csrf
< input type=”text” name=”username” placeholder = “enter user id” /> < br>
< input type=”password” name=”userpassword” placeholder = “enter user password” /> < br>
< button type=”submit”>Login < /button>
< /form>
</pre>
<p>And it will provide a page. It will just provide a security token for so that you cannot just make the grow site request, now try to login again after refresh the page and after submit now the output is as-:</p>
<p>“Form data will be here”</p>
<div class=”img2″>
<img src=”images/pic3.png” height=”150px” width=”400px”>
</div>
<p>But here you just get the data that you are just submitted from the file so for that first of all you need to a request in code, put “request” parameter in code in “userscontroller.php” file which belongs to http and according change the return().</p>
<pre>
< ?php
Namespace App\Http\Controllers;
Use Illuminate\Http\Request;
Class UsersController extends Controller
{
Function getData( Request $req)
{
Return $req->input();
}
}
</pre>
<p>Now go to browser or you can say output screen and refresh the page and now try to login again and just submit it again and output will be -:</p>
<div class=”img3″>
<img src=”images/pic2.png” height=”200px” width=”500px”>
</div>
<p>As you can see that you got the token- username and password.</p>
<p>So this is the way you put the data in form and get the data from php form to page html form to Laravel controller.</p>
</body>

Copyright 1999- Ducat Creative, All rights reserved.