How to show errors in client side in Laravel - php

How can I show errors in client side with Laravel controller in Laravel 4.2?
I want to show this error:
D:\xampp\htdocs\guestlara\app\views\index.php(51): Illuminate\Exception\Handler->handleError(8, 'Undefined varia...', 'D:\xampp\htdocs...', 51, Array)
public function login_user() {
$rules = array(
'email'=> 'Required|Between:3,64|Email|Unique:users',
'password'=>'Required|AlphaNum|Between:4,8|Confirmed',
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
// get the error messages from the validator
$messages = $validator->messages();
log::error($messages);
// redirect our user back to the form with the errors from the validator
return Redirect::to('/')->withErrors($validator);
} else {
// attempt to do the login
if (Auth::attempt($users)) {
return Redirect::to('dashboard');
} else {
return Redirect::to('/');
}
}
}
This is the HTML code:
<form action="login_user" method="post">
<div class="alert alert-info">
Message
</div>
<div class="form-group">
<label for="email">Email address</label>
<input class="form-control" name="email" placeholder="Enter Email Address" />
<div class="error"><?php echo $messages; ?></div>
</div>
<div class="form-group">
<label for="password">Password</label>
<input class="form-control" name="password" placeholder="Enter Password" />
<div class="error"><?php echo $messages; ?></div>
</div>
<input type="submit" name="submit" value="Login" class="btn btn-default">
<input type="button" name="register" value="Register" class="btn btn-default">
<input type="button" name="forgot-password" value="forgot-password" class="btn btn-default">
</form>

Related

Use of undefined constant title - assumed 'title'

I am pretty new in Laravel,
actually I am trying to create a crud operation using Laravel 5.6, so I have created create, delete function successfully, but on update function I am getting an error
Please find the attched image for detailed error
Use of undefined constant title - assumed 'title' (this will throw an Error in a future version of PHP)
Controller
public function edit($id){
$blogCategories = BlogCategories::find($id);
if (empty($blogCategories)) {
Flash::error('Category not found');
return redirect(route('categories.index'));
}
return view('cms/BlogCategories/editCategory')->with('blogCategories', $blogCategories);
}
public function update(Request $request, $id){
$blogCategories = BlogCategories::find($id);
$blogCategories->title = $request->get(title);
$blogCategories->slug = $request->get(slug);
$blogCategories->description = $request->get(description);
$blogCategories->featured_image = $request->get(featured_image);
$blogCategories->save();
return redirect()->back();
}
Model
class BlogCategories extends Model{
protected $fillable = ['title', 'slug', 'description', 'featured_image'];
protected $guarded = [];
}
Form
<form action="{{route('categories.update', $blogCategories->id)}}" method="post" class="m-form m-form--fit m-form--label-align-right">
#csrf
#method('put')
<div class="m-portlet__body">
<div class="form-group m-form__group">
<label>Title</label>
<input type="text" class="form-control m-input" name="title" id="title" value="{{$blogCategories->title}}" aria-describedby="emailHelp" placeholder="Muhammad Owais">
</div>
<div class="form-group m-form__group">
<label>slug</label>
<input type="text" class="form-control m-input" name="slug" id="slug" value="{{$blogCategories->slug}}" aria-describedby="emailHelp" placeholder="mail#domain.com">
</div>
<div class="form-group m-form__group">
<label>Description</label>
<textarea class="form-control" name="description" id="description" value="{{$blogCategories->description}}" placeholder="Enter Description"></textarea>
</div>
<div class="form-group m-form__group">
<label>Featured Image</label>
<input type="text" class="form-control m-input" name="featured_image" id="featured_image" value="{{$blogCategories->featured_image}}" aria-describedby="emailHelp" placeholder="Enter Amazon S3 URL">
</div>
</div>
<div class="m-portlet__foot m-portlet__foot--fit">
<div class="m-form__actions">
<button type="submit" class="btn btn-primary">
Submit
</button>
<button type="reset" class="btn btn-secondary">
Cancel
</button>
</div>
</div>
</form>
Use quotes for your all HTTP requests like :
$request->get('title');
$request->get('slug');

user_validation function is not invoked in Home controller in codeigniter

I need to validate a user form, which I am trying to submit after taking the input in view named as 'home.php', where I have specified the base_url('Home/user_validation').
With Home is name of the controller and user_validation is the method name. I tried to figure out the issue but I could not understand why not any other function is being invoked apart from index function in Home controller. Please help me in this case.
Controller Home.php
public function user_validation()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'trim|required');
if ($this->form_validation->run() == FALSE) {
$this->session->set_flashdata('message', 'Invalid username and password.');
redirect('home');
}
else {
$user_name123 = $this->input->post('username');
$teacher = $this->input->post('remember');
if ($user_name123 == 'admin') {
$query = $this->login_model->validate();
echo("Logged in");
}
}
}
Model login_model.php
function validate()
{
$this->db->where('admin_username', $this->input->post('username'));
$this->db->where('admin_password', $this->input->post('password'));
$this->db->where('active_status', 'Yes');
$query = $this->db->get('cis_tbl_admin');
if ($query->num_rows() == 1) {
return true;
}
else {
return false;
}
}
View home.php
<form action="<?php echo base_url('Home/user_validation') ?>" method="post" id="instantform">
<fieldset>
<div class="input-prepend" title="Username" data-rel="tooltip">
<span class="add-on"><i class="icon-user"></i></span>
<input autofocus class="input-large span10"
name="username" id="username" type="text"
placeholder="user name"/>
</div>
<div class="clearfix"></div>
<div class="input-prepend" title="Password" data-rel="tooltip">
<span class="add-on"><i class="icon-lock"></i></span>
<input class="input-large span10" name="password"
id="password" type="password"
placeholder="password" autocomplete="off"/>
</div>
<div class="clearfix"></div>
<div class="input-prepend">
<label class="remember" for="remember">
<input type="checkbox" name="remember" id="remember"
value="teacher"/>Teacher Login</label>
</div>
<div class="clearfix"></div>
<p class="center span5">
<button type="submit" name="submit" id="submit" class="btn btn-primary">Login</button>
<br/><br/>
<!--<a class="ajax-link" href="<?php echo base_url(); ?>parent_info/get_students_info"> <span class="hidden-tablet">Parent Login</span></a>-->
</p>
</fieldset>
<input type="hidden" name="sys_date" id="sys_date" value="<?php echo $sys_date; ?>">
<input type="hidden" name="sys_time" id="sys_time" value="<?php echo $sys_time; ?>">
<input type="hidden" name="details" id="details" value="<?php echo $details; ?>">
</form>
Check your config routes file to be sure you call functions of the home controller:
$route['Home/(:any)'] = 'Home/$1';

Codeigniter CSRF error: “action requested not allowed.”

My site was working just fine, no issues, then all of a sudden I receive error "An Error Was Encountered - The action you have requested is not allowed" during every form submit.
I am using Codeigniter 3 with SSL and I have CSRF protection turned on. I did not change anything lately so I have no idea why my site stopped working. If I turn CSRF off, form submission works fine.
Settings
$config['base_url'] = 'https://www.mywebsite.com';
$config['index_page'] = '';
$config['cookie_prefix'] = '';
$config['cookie_domain'] = '.mywebsite.com';
$config['cookie_path'] = '/';
$config['cookie_secure'] = FALSE;
$config['cookie_httponly'] = FALSE;
$config['csrf_protection'] = TRUE;
$config['csrf_token_name'] = 'msmm_tn';
$config['csrf_cookie_name'] = 'msmm_cn';
$config['csrf_expire'] = 7200;
$config['csrf_regenerate'] = TRUE;
$config['csrf_exclude_uris'] = array();
Form
<form action="https://www.mywebsite.com/" class="navbar-form navbar-left" id="search" method="post" accept-charset="utf-8">
<input type="hidden" name="msmm_tn" value="caeea1bc65c09f29691a1a692e09a30a" style="display:none;" />
<input type="hidden" name="token" value="ada2832a" style="display:none;" />
<input type="hidden" name="date-search-form" id="date-search-form" value="X">
<div class="form-group">
<input type="date" class="form-control input-md" name="from_date" id="from_date" value="2017-08-15" />
</div>
<div class="form-group">
<input type="date" class="form-control input-md" name="to_date" id="to_date" value="2017-08-16" />
</div>
<div class="form-group">
<input type="search" class="form-control input-md" name="search_text" id="search_text" placeholder="Search" value="">
</div>
<div class="form-group">
<button type="submit" class="btn btn-default btn-sm" name="go" id="go"><i class="fa fa-search" aria-hidden="true"></i>
</button>
</div>
</form>
</div>
Server side form code:
<?php
$attributes = array('class' => 'form-horizontal', 'id' => 'signup');
echo form_open('', $attributes);
?>
<?php
if( isset( $login_error_mesg ) )
{
echo '<div class="alert alert-danger input_bump" role="alert">';
echo 'Invalid Username, Email Address, or Password.<br>Username, email address and password are all case sensitive.';
echo '</div>';
}
if( $this->input->get('logout') )
{
echo '<div class="alert alert-success input_bump text-center" role="alert"><p>You have successfully logged out.</p></div>';
}
?>
<div class="form-group form-padding">
<label class="form-label" for="login_name">User Name or Email</label>
<input type="text" class="form-control form-fixer input-md" name="login_string" id="login_string" value="<?php echo $this->input->post('login_string'); ?>">
</div>
<div class="form-group form-padding">
<label class="form-label" for="pwd">Password</label>
<input type="password" class="form-control input-md" id="login_pass" name="login_pass" maxlength="<?php echo config_item('max_chars_for_password'); ?>" autocomplete="off" />
</div>
<div class="input_bump">
<?php
$link_protocol = USE_SSL ? 'https' : NULL;
?>
<p>
Can't access your account?
</p>
</div>
<div class="form-group form-padding">
<button type="submit" class="btn btn-success btn-full">Sign In</button>
</div>
<?php echo form_close(); ?>
Just add the Controller uri in:
$config['csrf_exclude_uris'] = array();
//e.g. $config['csrf_exclude_uris'] = array('register/add_member');
Where register is your controller and add_member is the method.

Login failed with php oop

I am new to PHP OOP, from the tutorial I am following, I was able to create a class that controls log in, sign up, update, profile and change password. I imported the class to my new web project it registers users but doesn't allow them to log in. Please anyone can help me figure out why the log in failed?
<?php
require_once 'core/init.php';
if(Input::exists()) {
if(Token::check(Input::get('token'))) {
$validate = new Validate();
$validation = $validate->check($_POST, array(
'username' => array('required' => true),
'password' => array('required' => true)
));
if($validation->passed()) {
$user = new User();
$remember = (Input::get('remember') === 'on') ? true : false;
$login = $user->login(Input::get('username'), Input::get ('password'),
$remember);
if($login) {
header('location: index.php');
//Redirect::to('index.php');
}else{
echo '<p><span class= "error">Sorry, logging in failed.</span></p>';
}
} else {
foreach($validation->errors() as $error) {
//echo "<span class=
\"error\">$error,</span><br>";
}
}
}
}
?>
HTML5
<html>
<head>
</head>
<body>
<div class="col-md-9">
<form class="form-horizontal" role="form" method="POST" >
<h3>SIGN IN PAGE</h3>
<div class="form-group">
<label for = "inputUsername" class = "col-sm-2 control- label">Username:</label>
<div class="col-sm-10">
<input class="form-control" id="focusedInput"
type="text" name="username" placeholder="Enter username...">
</div>
</div>
<div class="form-group">
<label for="inputPassword" class="col-sm-2 control-
label">Password:</label>
<div class="col-sm-10">
<input class="form-control" id="focusedInput"
type="password" name="password" placeholder="Enter last name..." >
</div>
</div>
<div class="checkbox">
<label for = "checkbox" class = "col-sm-4 control-
label">
<input type="checkbox" name= "remember"
id= "remember"> Remember me!
</label>
</div>
<p>
<input type= "hidden" name= "token" value= "<?php echo
Token::generate(); ?>">
<input type="submit" class="btn btn-primary btn-lg pull-
right" id= "submit" value="Sign in" name "submit">
</p><br>
<div class = "col-sm-4 control-label">
<p>Change password</p>
<p>Logout</p>
</div>
</form>
</div>
</div>
<!-- /.row --><br><br>
<hr>
</body>
</html>
`

Laravel User Registration Issue

I am trying to make a simple user registration form using laravel4. I am currently getting this error when I submit the form:
Symfony\Component\Debug\Exception\FatalErrorException thrown with message "Undefined class constant 'rules'"
Stacktrace:
#1 Symfony\Component\Debug\Exception\FatalErrorException in C:\wamp\www\growinlove.tld\app\controllers\UsersController.php:12
#0 Illuminate\Exception\Handler:handleShutdown in <#unknown>:0
This is my Controller:
class UsersController extends BaseController {
//Run when user visits site...Load a view
public function index(){
return View::make('home.index');
}
//Handles processing of registration form data
public function postCreate(){
$validator = Validator::make(Input::all(), User::rules);
if($validator->passes()){
//Save in DB - Success
$user = new User;
$user->fname = Input::get('fname'); //Get the details of form
$user->lname = Input::get('lname');
$user->email = Input::get('email');
$password->password = Hash::make(Input::get('password'));//Encrypt the password
$user->save();
return Redirect::to('/books')->with('Thank you for Registering!');
}else{
//Display error - Failed
return Redirect::to('/')->with('message', 'The Following Errors occurred')->withErrors($validator)->withInput();
}
}
}
This is my View:
<!-- Register Form -->
<form action="{{ action('UsersController#postCreate') }}" method="post" role="form">
<h2 class="form-signup-heading">Register</h2>
<!-- Display Errors -->
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
<!-- First Name -->
<div class="form-group">
<label>First Name</label>
<input type="text" class="form-control" name="fname" />
</div>
<!-- Last Name -->
<div class="form-group">
<label>Last Name</label>
<input type="text" class="form-control" name="lname" />
</div>
<!-- Email -->
<div class="form-group">
<label>Email</label>
<input type="text" class="form-control" name="email" />
</div>
<!-- Password-->
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" name="password" />
</div>
<!-- Confirm Password -->
<div class="form-group">
<label>Confirm Password</label>
<input type="password" class="form-control" name="password_confirmation" />
</div>
<input type="submit" value="Register" class="btn btn-primary"/>
</form>
This is my Model:
public static $rules = array(
'firstname'=>'required|alpha|min:2',
'lastname'=>'required|alpha|min:2',
'email'=>'required|email|unique:users',
'password'=>'required|alpha_num|between:6,12|confirmed',
'password_confirmation'=>'required|alpha_num|between:6,12'
);
use UserTrait, RemindableTrait;
The problem is at the line 12 of UserController.
Instead User::rules use User::$rules.
Replace in your controller
$validator = Validator::make(Input::all(), User::rules);
with
$validator = Validator::make(Input::all(), User::$rules);

Categories