HOW TO CREATE & GATHER FORM INPUT TO DISPLAY ON ANOTHER PAGE IN LARAVEL


HOW TO CREATE A FORM &GATHER FORM INPUT TO DISPLAY ON ANOTHER PAGE IN LARAVEL
HOW TO CREATE A FORM &GATHER FORM INPUT TO DISPLAY ON ANOTHER PAGE IN LARAVEL

HOW TO CREATE A SIMPLE FORM IN LARAVEL

A standout amongst the most essential parts of any web application is the frame. Laravel gives simple approaches to build HTML for our structures.

Use this code to create simple form in Laravel:

1.  In the app/viewsfolder, create a new userform.phpfile.
2.  In routes.php, create a route to load the view:
Route::get(userform, function()
{
return View::make('userform');
});
3.  In the userform.phpview, create a form using the code below:
<h1>User Info</h1>
<?= Form::open() ?>
<?= Form::label('username', 'Username') ?>
<?= Form::text('username') ?>
<br>
<?= Form::label('password', 'Password') ?>
<?= Form::password('password') ?>
<br>
<?= Form::label('color', 'Favorite Color') ?>
<?= Form::select('color', array('red' => 'red', 'green' =>
'green', 'blue' => 'blue')) ?>
<br>
<?= Form::submit('Send it!') ?>
<?= Form::close() ?>
View your form in the web page, by going to http://{your-server}/userform(where
{your-server}is the name of your server).

HOW TO GATHER FORM INPUT TO DISPLAY ON ANOTHER PAGE IN Laravel

After a user submits a form, we should have the capacity to take that information and pass it to another page. This formula demonstrates how we can utilize Laravel's worked in techniques to deal with our POST data.

Follow these steps to gather form input to display on another page:

1.  Create a route to handle the POST data from the form:
Route::post('userform', function()
{
// Process the data here
return Redirect::to('userresults')->withInput(Input::only('username', 'color'));
});
2.  Create a route to redirect to, and to display the data:
Route::get('userresults', function()
{
return 'Your username is: ' . Input::old('username')
. '<br>Your favorite color is: '
. Input::old('color');
});

HOW TO VALIDATE USER INPUT

In most web applications, there will be sure frame handle that are required to prepare the frame. We need to make sure that all the email locations are designed accurately. Or the input must have a specific number of characters. Utilizing Laravel's Validator class, we can check for these tenets and let the client know whether something is not right.

Follow the below steps to Validate user input in Laravel:

1.  Create a route to hold the form:
Route::get('userform', function()
{
return View::make('userform');
});
2.  Create a view named userform.php and add a form:
<h1>User Info</h1>
<?php $messages = $errors->all('<p
style="color:red">:message</p>') ?>
<?php
foreach ($messages as $msg)
{
echo $msg;
}
?>
<?= Form::open() ?>
<?= Form::label('email', 'Email') ?>
<?= Form::text('email', Input::old('email')) ?>
<br>
<?= Form::label('username', 'Username') ?>
<?= Form::text('username', Input::old('username')) ?>
<br>
<?= Form::label('password', 'Password') ?>
<?= Form::password('password') ?>
<br>
<?= Form::label('password_confirm', 'Retype your Password')
?>
<?= Form::password('password_confirm') ?>
<br>
<?= Form::label('color', 'Favorite Color') ?>
<?= Form::select('color', array('red' => 'red', 'green' =>
'green', 'blue' => 'blue'), Input::old('color')) ?>
<br>
<?= Form::submit('Send it!') ?>
<?php echo Form::close() ?>
3.  Create a route that handles our POST data and validates it:
Route::post('userform', function()
{
$rules = array(
'email' => 'required|email|different:username',
'username' => 'required|min:6',
'password' => 'required|same:password_confirm'
);
$validation = Validator::make(Input::all(), $rules);
if ($validation->fails())
{
return Redirect::to('userform')->withErrors($validation)->withInput();
}
return Redirect::to('userresults')->withInput();
});
4.  Create a route to handle a successful form submission:
Route::get('userresults', function()
{
return dd(Input::old());

});

Comments