laravel, auto-fill form fields from URL parameters - php

I'm trying to figure out how to properly set URL parameters to auto fill form fields in laravel
The form works and properly submits data but I have a URL like testsite.com/register?email=email#test.com&password=dK94*DFj
and I'd like to auto fill the form with the email and password from the URL
the form:
<form method="POST" action="{{ route('auth.register') }}">
<div class="reg-card reg-margin-bottom">
<div class="reg-card-content">
<div class="form-group">
<label for="email">Email Address</label>
<input type="email" class="form-control" id="email" name="email" required value="{{ $invitation->email }}" />
</div>
<div class="form-group">
<label for="password">New Password</label>
<input type="password" id="password" class="form-control" required name="password" />
</div>
</div>
</div>
<div class="reg-card">
<div class="reg-card-content">
<button type="submit" class="btn btn-primary btn-block">Create account</button>
</div>
</div>
</form>
Can I set this so that, as long as the params exist in the URL, then they autofill and disable those fields?

You can get it by using
{{ request()->email }} and {{ request()->password }}
However, passing password in the url is no a good idea though!

if you want a cleaner URL, uou can create a route for the URl register.
Route::get('/register/{email?}/{hash?}',function($email,$hash){
return view("viewFile",['email'=>$email,'password'=>$hash]);
});
In your view
<form method="POST" action="{{ route('auth.register') }}">
<div class="reg-card reg-margin-bottom">
<div class="reg-card-content">
<div class="form-group">
<label for="email">Email Address</label>
<input type="email" class="form-control" id="email" name="email" required value="{{ $email }}" />
</div>
<div class="form-group">
<label for="password">New Password</label>
<input type="password" id="password" class="form-control" required name="password" value="{{$password}}" />
</div>
</div>
</div>
<div class="reg-card">
<div class="reg-card-content">
<button type="submit" class="btn btn-primary btn-block">Create account</button>
</div>
</div>
</form>
I don't recommend you to pass a password in the url, but if it is a hash to validate that the email is the correct one, is a good idea I do it when I want to verified that the info is not changed by the user. Also this will need to hash the email and compare the hash with the one on the url.

Related

Basic HTML Link Send PHP Variable

This is my first go at trying to create a secure login feature.
Right now I have a mysql database storing a few usernames and passwords.
I also have a bootstrap template for a login page.
Here is some of the html:
<form>
<div class="form-group">
<div class="form-label-group">
<input type="email" id="inputEmail" class="form-control" placeholder="Email address" required="required" autofocus="autofocus">
<label for="inputEmail">Email address</label>
</div>
</div>
<div class="form-group">
<div class="form-label-group">
<input type="password" id="inputPassword" class="form-control" placeholder="Password" required="required">
<label for="inputPassword">Password</label>
</div>
</div>
<a class="btn btn-primary btn-block" href="connect.php" method="post">Login</a>
</form>
I added the method and changed the action for the login button/link.
I would like to be able to click the login link and have the input values sent to an external php program connect.php shown below:
<!DOCTYPE html>
<html lang="en">
<body>
<div>
<?php
$user = $_POST["inputEmail"];
$pass = $_POST["inputPassword"];
echo "<h1>Hello: ".$user." also:".$pass"</h1>";
?>
</div>
</body>
</html>
Once I can get the values to send to the external script, I can then start to check the values against those in my database.
Solutions I have seen are mostly dealing with the form action and not a link. Those that are dealing with a link use get and hard code the values being sent rather than input.
EDIT 1:
I realized my html page was launching outside of my server. I made some changes to my html:
<form method="post">
<div class="form-group">
<div class="form-label-group">
<input type="email" id="inputEmail" name="inputEmail" class="form-control" placeholder="Email address" required="required" autofocus="autofocus">
<label for="inputEmail">Email address</label>
</div>
</div>
<div class="form-group">
<div class="form-label-group">
<input type="password" id="inputPassword" name="inputPassword" class="form-control" placeholder="Password" required="required">
<label for="inputPassword">Password</label>
</div>
</div>
<a class="btn btn-primary btn-block" href="connect.php" method="post" name="submit">Login</a>
</form>
after these changes its telling me my inputs are Unidentified index's
SOLUTION
change submit link to submit button & make sure to run on a server
<form action="connect.php" method="POST">
<div class="form-group">
<div class="form-label-group">
<input type="email" id="inputEmail" name="inputEmail" class="form-control" placeholder="Email address" required="required" autofocus="autofocus">
<label for="inputEmail">Email address</label>
</div>
</div>
<div class="form-group">
<div class="form-label-group">
<input type="password" id="inputPassword" name="inputPassword" class="form-control" placeholder="Password" required="required">
<label for="inputPassword">Password</label>
</div>
</div>
<button class="btn btn-primary btn-block" name="submit" type="submit">Login</button>
</form>

Is it possible to use Auth::route() as authenticator for login other than default laravel login page?

I am curious to know whether Laravel Auth routing can be use outside of the default login/register page. If there is 2 different login pages in a project, is it possible to handle both request by Auth::route().
2nd login page be like:
<form class="form" role="form" method="post" action="{{ route('login') }}" accept-charset="UTF-8" id="login-nav">
<div class="form-group">
<label class="sr-only" for="LogInEmail">Email address</label>
<input type="email" class="form-control" id="LogInEmail" placeholder="Email address" required>
</div>
<div class="form-group">
<label class="sr-only" for="LogInPassword">Password</label>
<input type="password" class="form-control" id="LogInPassword" placeholder="Password" required>
<div class="help-block text-right">
Forget the password ?
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-block blue-gradient z-depth-1a">Log in <i class="fas fa-sign-in-alt ml-1"></i></button>
</div>
<div class="checkbox">
<label>
<input type="checkbox"> keep me logged-in
</label>
</div>
</form>
I am showing only 1 login because php artisan make:auth gives you the default one. What changes need to make if I want to pass this form data to Auth::route() ?
Thanks
Auth::route() is syntactic sugar for routes Laravel bootstrap. This answer shows the workings. You could just copy the files that Auth::routes() creates and edit them as suites you.

How To pre populate Html form field with laravel?

i want to pre fill my html form field "Reference" from url value for referne is afer ref?=1234 how to do it with laravel
<form method="post" accept-charset="utf-8" class="block" enctype="multipart/form-data" action="{{ route('register') }}">
{!! csrf_field() !!}
<div class="row">
<div class="col-md-12 col-sm-12">
<div style="width: 100%" class="alert alert-warning">
Please Register With a Reference User.
</div>
</div>
</div>
<div class="input-group">
<input type="text" name="reference" value="HERE WANT TO SHOW REFEERENCE CODE FROM URL" required class="form-control" placeholder="Enter Reference ID *" aria-describedby="basic-addon1">
<span class="input-group-addon" id="basic-addon1"><i class="fa fa-handshake-o"></i></span>
</div>
You can use like:
<input type="text" name="reference" value="{{ app('request')->input('ref') }}" required class="form-control" placeholder="Enter Reference ID *" aria-describedby="basic-addon1">
Use request() helper:
$ref = request('ref'); // It will return 1234.
There isn't a $GET variable in PHP.
There is a $_GET.
In laravel, you may use the Request facade or Input facade
so here:
<input value="view{{ \Request::get('ref') }}" type="text" name="reference" required class="form-control" placeholder="Enter Reference ID *" aria-describedby="basic-addon1">
// http://localhost/codec/register?ref=g5M089zQeVzc
// will produce
// <input value="viewg5M089zQeVzc" ...

Passing data from form to controller Laravel 5

I'm trying register users in a laravel 5 application using the restful controller.
The problem is that when I dump the data in my store function, I only get the csrf token, but not the values.
Here's what I tried so far:
Input::all();
Request::get();
Here's the code I'm executing:
Form
<form class="form-horizontal" method="post" action="/users">
<div class="form-group">
<label for="name" class="col-lg-2 control-label">
Name
</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="name" value="a">
</div>
</div>
<div class="form-group">
<label for="email" class="col-lg-2 control-label">
Email
</label>
<div class="col-lg-10">
<input type="email" class="form-control" id="email" value="a#a.Fr">
</div>
</div>
<div class="form-group">
<label for="password" class="col-lg-2 control-label">
Pw
</label>
<div class="col-lg-10">
<input type="password" class="form-control" id="password">
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button type="reset" class="btn btn-default">Cancel</button>
<button type="submit" class="btn btn-primary">Create</button>
</div>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
Controller
public function store()
{
$input = Input::only('name','email','password');
$user = new User;
$user->name = $input['name'];
$user->email = $input['email'];
$user->password = Hash::make($input['password']);
$user->save();
}
And Route is just
Route::resource('users','UsersController');
Your input fields are all missing a name attribute! For example
<input type="text" class="form-control" id="name" name="name" value="a">
<!-- ^^^^^^^^^^^ -->
You should put name attributes on the input fields, because html forms communicate with php with name attribute.
Example:
<input........... name="etc">
and in controller use the name you have given an input as a key :
$user->name = $input['etc'];
<input type="text" class="form-control" id="name" name="name" value="a">

CodeIgniter Login with enter key

have this in my login form :
<div class="middle-box loginscreen animated fadeInDown">
<div>
<div>
<h1 class="logo-name"><?= $this->lang->line('GGG')?></h1>
</div>
<h3><?= $this->lang->line('GGG')?></h3>
<form class="m-t" role="form" action="welcome/check_user" id="check_user" method="POST">
<div id="save_result"></div>
<div class="form-group has-feedback">
<label class="control-label" for="username"><?= $this->lang->line('Username')?><sup class="mandatory">*</sup></label>
<input type="email" class="form-control required email" placeholder="<?= $this->lang->line('Username')?>" name="username" id="username" maxlength="50">
</div>
<div class="form-group has-feedback">
<label class="control-label" for="password"><?= $this->lang->line('Password')?><sup class="mandatory">*</sup></label>
<input type="password" id="btn" class="form-control required" placeholder="<?= $this->lang->line('Password')?>" name="password" id="password">
</div>
<button type="button" onclick="submit_form('#check_user')" class="btn btn-primary block full-width m-b"><?= $this->lang->line('Login')?></button>
need to get enter button to submit and it wont.
Change button type to submit instead of button - browsers would the submit the form on enter
<button type="submit" onclick="submit_form('#check_user')" class="btn btn-primary block full-width m-b"><?= $this->lang->line('Login')?></button>

Categories