So I have a form on the front page:
{{ Form::open(array('url' => URL::to('', array(), true))) }}
<p>
{{Form::label('author') }}
{{Form::text('author') }}
</p>
<p>
{{Form::label('title') }}
{{Form::text('title') }}
</p>
<p>
{{Form::label('message') }}
{{Form::text('message') }}
</p>
<p>
{{ Form::submit() }}
</p>
{{ Form::close() }}
Then these are my routes:
Route::get('/', function()
{
/* unrelated stuff here */
});
Route::post('/', function()
{
/* testing */
print_r("DONE!");
exit;
});
So I am basically trying to test whether my form submission works or not. And it doesn't. For some reason the POST method is being executed once I load the front page instead of when the form is submitted. Why is that and how can I fix this?
Your form url should be specified to post to / -
{{ Form::open(array('url' => '/')) }}
In order to redirect back to the homepage you should modify your post method like this -
Route::post('/', function()
{
/* testing */
print_r("DONE!");
return Redirect::to('/');
});
Here is the documentation for redirecting.
I would recommend you to use dirrefent route:
{{ Form::open(array('url' => URL::to('/submitForm', array(), true))) }}
And then:
Route::post('/submitForm', function()
{
/* testing */
print_r("DONE!");
return redirect()->route('/');
});
Related
I have created a Form in a blade template. This form sends a POST request to search.postQuery, so I can get the search query and then do something with it and return a View.
This is the route I have defined:
Route::post('/search/{query}', ['as' => 'search.postQuery', 'uses' => 'SearchController#postQuery'])->where('query', '[a-zA-Z0-9]+');
My form looks like this:
{{ Form::open(array('method' => 'POST', 'route' => array('search.postQuery')) }}
{{ Form::text('searchQuery') }}
{{ Form::submit('Zoeken!') }}
{{ Form::close() }}
This is the method the route calls on POST:
public function postQuery($query)
{
var_dump("Landed here");
}
And finally, the error Laravel poses me with is a NotFoundHttpException.
I also discovered that Laravel is constructing a rather strange URL when I press submit: http://homestead.app/search/%7Bquery%7D
What am I doing wrong? As to my knowledge, I'm not doing something very strange?
This is your error
{{ Form::open(array('route'=>'search.postQuery','method' => 'POST')) }}
{{ Form::text('searchQuery') }}
{{ Form::submit('Zoeken!') }}
{{ Form::close() }}
Im a beginner with laravel. Im following the laracasts tutorial and Im stuck at the part where you access another page with a form by:
{{ Form::open(['url' => 'created']) }}
for example.
Now that leads me to the right url but it gives me
Whoops, looks like something went wrong.
As soon as I type the link manually it works normally.
This is the code of the page where it directs to:
controller:
public function created()
{
return 'hello';
}
Routes:
Route::get('created', 'TestController#created');
View:
#extends('layout')
#section('content')
<h1> Test </h1>
#stop
This is the form of the 1st page:
#extends('layout')
#section('content')
<h1>Create New User</h1>
{{ Form::open(['url' => 'created']) }}
<div>
{{ Form::label('email', 'E-mail:')}}
{{ Form::text('email')}}
</div>
<div>
{{ Form::label('password', 'Password:')}}
{{ Form::password('password')}}
</div>
<div>
{{ Form::submit('Create')}}
</div>
{{ Form::close()}}
#stop
What is going wrong here?
Form open by default links to a post method so what you need is either a post route or a get method. Following should work:
{{ Form::open(['url' => 'created']) }}
// Insert your fields/codes here
{{ Form::close() }}
//Change route method to post
Route::post('created', 'TestController#created');
Please read the documentation here for more details.
I am trying to make a loginbox with jquery using the laravel framework. I have rules in Laravel which i want it to follow. here the code for that:
$rules = array(
'username' => 'required|alphaNum|min:6|max:16',
'password' => 'required|alphaNum|min:6'
);
They work fine, and when you fail with your login the following code is run:
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::to('index')
->withErrors($validator)
->withInput(Input::except('password'));
The problem is that the log in-box pops up when called by pressing a button, which becomes a problem when I redirected to another page, because the error messages are written inside the log in box. This is good, however i cant get the log in box to pop up automatically once redirected, so you have to click the log in button again to see the error message and try again.
Here is the jQuery i use:
$(function(){
$('#loginform').submit(function(e){
return false;
});
$('#modaltrigger').leanModal({ top: 110, overlay: 0.45, closeButton: ".hidemodal" });
});
And here is the HTML log in form:
<div id="loginmodal" style="display:none;">
<h1>Logga in</h1>
{{ Form::open(array('url' => 'login')) }}
<p>
{{ $errors->first('username') }}
{{ $errors->first('password') }}
</p>
<div class="row">
<div class="col-md-12">
{{ Form::label('username', 'Username', array('class' => 'label')) }}
{{ Form::text('username', Input::old('username'), array('placeholder' => 'BTH Akronym',
'class' => 'textfield',
'id' => 'login-margin',
))
}}
</div>
<div class="col-md-12">
{{ Form::label('password', 'Password', array('class' => 'label')) }}
{{ Form::password('password', array('placeholder' => 'Lösenord',
'class' => 'textfield'
)) }}
</div>
</div>
<div class="center">
{{ Form::submit('Logga in', array('class' => 'login-knapp')) }}
</div>
{{ Form::close() }}
</div>
This is untested, but something like this should work for you...
PHP
if ($validator->fails()) {
if(Request::ajax()) {
return Response::json($validator->getMessages());
} else {
return Redirect::to('index')->withErrors($validator)->withInput(Input::except('password'));
}
}
The idea here is if the request is an ajax request, it will just return some json which we will be able to use on the success function of an ajax request on your template.
jQuery
$("#form").submit(function(e) {
$.ajax({
type: "POST",
url: "{{ route('ajax.form_submit') }}",
data: $("#form").serialize(), // serializes the form's elements.
success: function(data) {
if(data.length > 0) {
console.log(data); // Can set your messages on your modal.
e.preventDefault(); // Stop the form from submitting.
}
}
});
});
If there are any error messages, it will log them to the console and give you a good idea of what else needs to be done. If there are no error messages, it should just continue on with the form submission.
On $validator->fails(), I'd probably put an else on there for a success which would actually log the user in. This way because the function will run twice if it was a successful attempt (once by ajax, once by the form actually submitting), it wouldn't have to run the validator or check the database again (I'm assuming you already have a filter in place on these so it won't run if the user is logged in).
I have problem with action in {{ Form::open() }}
Route [adm/tagedit] not defined.
My code in view is
{{ Form::open(['method'=>'post','action'=>"adm/tagedit"])}}
{{ Form::submit('edit',['class'=>'btn btn-default']) }}
{{ Form::close() }}
and in routes
Route::get('adm/{action?}/{params?}',function($action,$params=null){
if(Auth::check()==false||Auth::user()->isAdmin()==false){
return \Illuminate\Support\Facades\Redirect::to('/')->withError('Youe need be logged in');
}
return (new AdmController())->{$action}($params);
});
//routing for bacend post method
Route::post('adm/{action?}/{params?}',['before'=>'csrf',function($action,$params=null){
if(Auth::check()==false||Auth::user()->isAdmin()==false){
return \Illuminate\Support\Facades\Redirect::to('/')->withError('You need be logged in');
}
return (new AdmController())->{$action.'Post'}($params);
}]);
Of course if I use get request action work. In controller I use action "tageditPost"
Probably my problem is similar to Rediredt:route() is not work too. But in redirect I use Reditect:to() and work fine. In form, I don't know what should I change.
Thanks in advance for answers.
Regards
Try the following:
{{ Form::open(['method'=>'post','url'=>"adm/tagedit"]) }}
{{ Form::submit('edit',['class'=>'btn btn-default']) }}
{{ Form::close() }}
I'm facing a weird issue while developing a very basic application using Laravel 4. I have created the functionality following the documentation of laravel 4's user authenticating docs where users can sign up to ask different questions and only the registered users can access to the secret/admin page. Everything works well so far except that I'm getting this weird issue where even after logging out users can still access to the precious page or in other word the admin page. I'm not sure it has something to do with Laravel but still I couldn't figure out what's the issue and how to prevent or force the browser to reload or something so that they can't see the admin page even if they click on the back arrow in browser.
Although it may not be relavent but I'm still attaching the codes I have. In routes.php I have this
<?php
Route::get('/', array('as'=>'home', 'uses'=>'QuestionController#getindex'));
Route::get('register', array('as'=>'register', 'uses'=>'UserController#getnew'));
Route::get('login', array('as'=>'login', 'uses'=>'UserController#getlogin'));
Route::get('logout', array('as'=>'logout', 'uses'=>'UserController#getlogout'));
Route::post('register', array('before'=>'csrf', 'uses'=>'UserController#postcreate'));
Route::post('login', array('before'=>'csrf', 'uses'=>'UserController#postlogin'));
And in userController I have this
<?php
class UserController extends BaseController {
public function getNew() {
return View::make('users.new')
->with('title', 'Snappy Q&A - Register');
}
public function postCreate() {
$validator = Member::validate(Input::all());
if ( $validator->passes() ) {
$user = User::create( array (
'username' => Input::get('username'),
'password' => Hash::make(Input::get('password'))
));
Auth::login($user);
return Redirect::route('home')->with('message', 'Thanks for registering!');
} else {
return Redirect::route('register')->withErrors($validator)->withInput();
}
}
public function getLogin() {
return View::make('users.login')
->with('title', 'Snappy Q&A - Login');
}
public function postLogin() {
$user_creds = array(
'username' => Input::get('username'),
'password' => Input::get('password')
);
if( Auth::attempt($user_creds) ) {
return Redirect::route('home')
->with('message', 'Yep, you are now logged in');
} else {
return Redirect::route('login')
->with('message', 'Shit man! The creds are not authorised!')
->withInput();
}
}
public function getLogout() {
if( Auth::check() ) {
Auth::logout();
return Redirect::route('login')
->with('message', 'You are now logged out!');
} else {
return Redirect::route('home');
}
}
}
This new.blade.php is responsible for creating a new user
#extends('master.master')
#section('content')
#if( $errors->has() )
<p>The following erros has occured: </p>
<ul class="form-errors">
{{ $errors->first('username', '<li>:message</li>') }}
{{ $errors->first('password', '<li>:message</li>') }}
{{ $errors->first('password_confirmation', '<li>:message</li>') }}
</ul>
#endif
{{ Form::open( array('route'=>'register', 'method'=>'post')) }}
{{ Form::token() }}
{{ Form::label('username', 'Username') }}
{{ Form::text('username', Input::old('username')) }}
{{ Form::label('password', 'Password') }}
{{ Form::password('password') }}
{{ Form::label('password_confirmation', 'Confirm Password') }}
{{ Form::password('password_confirmation') }}
{{ Form::submit('Register', array('class'=>'btn btn-success')) }}
{{ Form::close() }}
#stop
This one is for logging in a user:
#extends('master.master')
#section('content')
{{ Form::open( array('route'=>'login', 'method'=>'post') ) }}
{{ Form::token() }}
{{ Form::label('username', 'Username') }}
{{ Form::text('username', Input::old('username')) }}
{{ Form::label('password', 'Password') }}
{{ Form::password('password') }}
{{ Form::submit('Login', array('class' => 'btn btn-success')) }}
{{ Form::close() }}
#stop
TO make it clear, I don't have anything special yet for the admin page except the condition in the navigation with the method Auth::check(); to make sure that only logged in user can see the logout navigation. I will create the functionality later after I'm done with this problem. The admin page view will be in a folder called questions. I hope this makes sense now.
How do I take care of that? Please ask if you still need any other instance of my code. And I believe many of the newbies in Laravel world face more or less the same issue while developing functionality like that. I'm hoping this is a good question and will help others as well as me.
Try to use filters in routes
Route::get('/', array('before' => 'auth', function()
{
}));
More:
http://laravel.com/docs/routing#route-filters