I'm pretty new to the Laravel universe.
So I was following a tutorial on OpenClassrooms and the tutorial is about Laravel 4 and I'm using Laravel 5, so I went through some trouble trying to adapt my controllers and getting them to work on my project, and after getting rid of many errors concerning the namespace dependencies, I'm getting a form which looks like text html that is not processed, here's what's showing up:
<form method="POST" action="http://gappsl/users" accept-charset="UTF-8">
<input
name="_token"
type="hidden"
value="HMfnLvctXZqOuCpSdeJXML76L2KoPsZtadpIqOnm">
<label for="nom">Enter your name:</label>
<input name="nom" type="text" id="nom">
<input type="submit" value="Submit">
</form>
Here's my controller:
<?php namespace App\Http\Controllers; use \View;
class UsersController extends Controller {
public function getInfos() {
return View::make('infos');
}
public function postInfos() {
echo 'The name is ' . Input::get('nom');
}
}
?>
Here's my views:
#extends('tempform')
#section('content') {{ Form::open(array('url'=>'users')) }} {{ Form::label('nom', 'Enter your name:') }} {{ Form::text('nom') }} {{ Form::submit('Submit') }} {{ Form::close() }}
#stop
<!doctype html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<body>
<p> #yield('content') </p>
</body>
</html>
I hope you can help me.
In Laravel 5, {{ }} (for example {{ Form::open() }} escapes the variables in blade templates.
In Laravel 5, use {!! Form::open() !!} instead. Also, double check that it actually is a blade template you're using (and not a straight up PHP one).
for Laravel Framework version 5.2.34 this worked for me
{!! Form::open(array('url' => '/admin/expertise')) !!}
{!! Form::close() !!}
with one { and two !! .
Related
First question asked in this forum.
I have watched many tutorials on file uploading in laravel, did exactly what they did but file is not uploading. It would be great of you could help me.I am posting all the relevant codes for this.
Here is my html code for taking file input and other inputs
<div id="form" >
<div id="select" style="font-size:20px;">
{{ Form::open(['route' => 'gpa.science'])}}
<div id="youtubelink" style="font-size:20px;">
<p>শিরোনাম :</p>
<h22 > {{ Form::textarea('title', null, ['size' => '70x1']) }} </h22>
</div>
</br>
<div id="youtubelink" style="font-size:20px;">
<p>ইউটিউব ভিডিও লিঙ্ক :</p>
<h22 > {{ Form::textarea('videokey', null, ['size' => '70x1']) }} </h22>
</div>
</br>
<div>
<form action="" name="filea">
<input type="file" name="filea" enctype="multipart/form-data">
<input type="hidden" value="{{ csrf_token() }}" name="_token">
</div>
</form>
<div class="input-filed">
{{ Form::submit('Submit', ['class'=>'btn btn-primary right'])}}
{{ Form::close()}}
</div>
Here is my route
Route::post('/blog1', ['as'=>'gpa.science', 'uses' => 'PageController#blogafter']);
Now after submit button this will go to this PageController.
PageController Code:
<?php namespace App\Http\Controllers;
use DB;
use App\Quotation;
use Input;
use Illuminate\Http\Request;
use App\Filename; use Storage;
use Illuminate\Support\Facades\File;
use Illuminate\Http\UploadedFile;
class PageController extends Controller {
public function blogafter(Request $request){
//return $request->all();
if($request->hasFile('filea'))
{
dd('Got the file');
}
dd('No file');
return view('blogafter');
} }
Now the problem is it does not get any file. Always shows no file.If I do $request->all(); it returns
videokey null
filea "working.sql"
Now can anyone tell me what is wrong in my code? Why I can not upload files. I am using laravel 5.4.36 and php version 5.6.31
You have typo in your form which is missing enctype="multipart/form-data"
<form action="" name="filea" method="post" enctype="multipart/form-data" >
Add
{{ Form::open(['route' => 'gpa.science', 'files'=> true])}}
Hope this helps.
Change
{{ Form::open(['route' => 'gpa.science']) }}
to
{{ Form::open(['route' => 'gpa.science', 'files' => true]) }}
This will add enctype="multipart/form-data" to the form, which is required to upload files to PHP.
I know that this question may have been made but I just can't get it to work. if someone could help me I would be very grateful. I have colletive/form installed but the answer can be an html form tag too.
Now listing my form, my route and my exception.
{{ Form::model( array('route' => array('casas.update', 238), 'method' => 'PUT')) }}
<input type="hidden" name="_method" value="PUT">
-
Route::resource('casas', 'CasasController');
exception:
MethodNotAllowedHttpException in RouteCollection.php line 218:
With plain html / blade
<form action="{{ route('casas.update', $casa->id) }}" method="post">
{{ csrf_field() }}
{{ method_field('put') }}
{{-- Your form fields go here --}}
<input type="submit" value="Update">
</form>
Wirth Laravel Collective it may look like
{{ Form::model($casa, ['route' => ['casas.update', $casa->id], 'method' => 'put']) }}
{{-- Your form fields go here --}}
{{ Form::submit('Update') }}
{{ Form::close() }}
In both cases it's assumed that you pass a model instance $casa into your blade template
In your controller
class CasasController extends Controller
{
public function edit(Casa $casa) // type hint your Model
{
return view('casas.edit')
->with('casa', $casa);
}
public function update(Request $request, Casa $casa) // type hint your Model
{
dd($casa, $request->all());
}
}
I am making simple form of adding news in Laravel 5.4 backpack admin just to have overview of Laravel 5.4 but got stuck while posting data from News form located at news/add view. Though I am sending action to News Controller at add method but it is showing 405 method not allowed error. Please check my code below and let me know what is the issue in it. Might be I am doing some silly mistake, sorry if that is the case.
View : add.blade.php
{!! Form::open(['action' => 'NewsController#add']) !!}
<div class="form-group">
<label for="title">Title:</label>
<input name="title" id="title" type="text" class="form-control" required>
</div>
<div class="form-group">
<label for="description">Description:</label>
<textarea name="description" id="description" class="form-control">
</textarea>
</div>
<button class="btn btn-default" type="submit" name="submitBtn"
value="Submit">Submit</button>
{!! Form::close() !!}
Controller : NewsController.php
public function add(){
echo "<pre>"; print_r($this->data->request); die;
return view("news.add");
}
Firstly add this in top in your Controller:-
use Illuminate\Http\Request;
use App\Http\Requests;
After that your function should have this parmeter Request $request:-
public function add(Request $request){
$data = $request->all();
return view("news.add");
}
Hope it helps!
It seems like method in the route file is not POST.
Change your route like this :
Route::post('/addnews',['as' => 'news.add', 'uses'=>'NewsController#add']);
I would recommend you to use named route. It will be easy to use.
{!! Form::open(['route' => 'news.add']) !!}
Use this format:
{!! Form::open(array('url' => 'add')) !!}
// your form fields
{!! Form::close() !!}
Your route will be:
Route::post('/add','NewsController#add');
Hope it helps..
So I have written the following route:
Route::get('/login', function() {
return View::make('login.form');
});
This is the view:
#extends('layouts.master')
#section('content')
<div class="form-section">
{{ Form::open(
array(
'url' => 'login-submit',
'method' => 'POST'
)
)
}}
{{ Form::submit('Authorize With AisisPlatform') }}
{{ Form::close() }}
#stop
This is exactly what I see when I look at the page:
<form method="POST" action="http://app-response.tracking/login-submit" accept-charset="UTF-8"><input name="_token" type="hidden" value="7xHzX20h1RZBnkTP2CRraZVsAfSQIfVP61mBiFtN"> <input type="submit" value="Authorize With AisisPlatform"> </form>
Um..... Shouldn't the form be well .... and actual form? Why did it render out the html as a string? How do I make it render the actual form submit button?
The default echo braces: {{ ... }} escape HTML by default, to prevent HTML injection.
You should use {!! .. !!} to print raw HTML. For example:
{!! Form::submit('Authorize With AisisPlatform') !!}
I am setting up a simple form in laravel:
This is the route file:
Route::get('backoffice/upload', [ 'as' => 'backoffice/upload',
'uses' => 'UploadController#uploadForm']);
Route::post('backoffice/saveimage',[ 'as' => 'backoffice/saveimage',
'uses' => 'UploadController#saveImage']);
This is the controller:
class UploadController extends \BaseController
{
public function uploadForm()
{
return View::make("backoffice.upload.create");
}
public function saveImage()
{
return "Uploading...";
}
}
And this is the View file:
<h1>Upload Image</h1>
{{ Form::open(['action' => 'UploadController#saveImage']) }}
<div class='formfield'>
{{ Form::label('newfilename','New File Name (optional):') }}
{{ Form::input('text','newfilename') }}
{{ $errors->first('newfilename') }}
</div>
<div class='formfield'>
{{ Form::submit($action,['class'=>'button']) }}
{{ Form::btnLink('Cancel',URL::previous(),['class'=>'button']) }}
</div>
{{ Form::close() }}
// Generated HTML
<h1>Upload Image</h1>
<form method="POST" action="http://my.local/backoffice/saveimage" accept-charset="UTF-8"><input name="_token" type="hidden" value="x9g4SW2R7t9kia2B8HRJTm1jbLRl3BB8sPMwvgAM">
<div class='formfield'>
<label for="newfilename">New File Name (optional):</label>
<input name="newfilename" type="text" id="newfilename">
</div>
<div class='formfield'>
<input class="button" type="submit" value="Create">
</div>
</form>
So, if I go to: http://my.local/backoffice/upload I get the form with the HTML above.
However, if I type anything, then click SUBMIT, I return to the form but now have the following URL:
http://my.local/backoffice/upload?pz_session=x9g4SW2R7t9kia2B8HRJTm1jbLRl3BB8sPMwvgAM&_token=x9g4SW2R7t9kia2B8HRJTm1jbLRl3BB8sPMwvgAM&newfilename=ddd
This makes no sense to me. Up until now I have always used route::resource when dealing with forms, and had no problem. I am trying to do a simple form with GET and POST and am having no end of grief. What am I missing?
Furthermore, if I modify routes.php and change it from post to any, then open a browser window and type: http://my.local/backoffice/saveimage then I get the message "Uploading..." so that part is working ok.
Found the solution. In making the backoffice of the system, I had re-used the frontoffice template but removed all the excess. Or so I had thought. However, the front office header template had a form which I had only partially deleted.
So the problem was that there was an opening FORM tag I didn't know about. Consequently, when I clicked on submit to my form, it was actually submitting to this other form.
As the other form had no action it was default to itself.
Of course, had I just validated the HTML this would have shown up straight away. The lesson learned here is to validate my html before submitting questions!
Try this, and be sure to correctly configure your url at app/config/app.php
{{Form::open(['url'=>'backoffice/saveimage'])}}
//code
{{Form::close()}}