I have created a form that uploads a file but it returns a null value when I submit. When I add in the enctype="multipart/form-data" it reloads the page and doesn't seem to go through my controller.
MY HTML FORM
<form class="form-horizontal" role="form" name="importform" method="POST" action="{{ route('import_type') }}" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="control-group">
<label class="control-label"> </label>
<div class="controls">
<div class="control-group text-center">
<label class="btn btn-primary" for="file-selector">
<input id="file-selector" name="template_upload" type="file" value="" required autofocus style="display:none" onchange="$('#upload-file-info').html(this.files[0].name)" required> Upload List </label>
<span class='label label-default' id="upload-file-info"></span>
</div>
</div>
</div>
<div class="control-group">
<div class="controls">
<input class="btn btn-primary" type="submit" id="import-submit" name="import-submit">
</div>
</div>
</form>
MY CONTROLLER: I am using the import method
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\ImportTypeRequest;
use \App\Guest;
use \App\Role;
use \App\User;
use \App\Type;
use Illuminate\Support\Facades\Auth;
class GuestController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$user = User::with('roles')->where('id', Auth::id())->get();
$types = Type::where('user_id', Auth::id())
->where('active',1)->get();
return view('view_import',compact('user','types'));
}
public function import(ImportTypeRequest $request)
{
$template_upload = $request->file('template_upload');
dd($template_upload);
}
}
Here are some suggested ways trying to solve this.
First of all in your import method add dd($request->all()) at its top and see what's the response. You should see all your form data and of course template_upload file. That's how you make sure that you see all the coming data from your form to your controller method.
Then try to get rid of ImportTypeRequest and just use Illuminate\Http\Request to see what will you get. If you got different result then the problem is in ImportTypeRequest class.
Then why don't you just use $request->template_upload?! It's cleaner I guess.
Related
I encounter a problem that the data cannot be stored in the database. when I use the save function to update a column, there is no error reminder and does not insert as well. I have no idea. Help me, Bro.
I have a supervisor table that includes the id, user_id, name, email, expertise, and initial letter columns. the primary key is user_id.
this is my route for an edit view
Route::get('supervisor/profile/{id}/edit','UserController#supProfileEdit');
this is the view to post the new data.
Route::post('supervisor/profile/{id}','UserController#supProfileUpdate');
a blade is a form :
<div class="card">
<div class="card-header bg-primary text-white">Profile</div>
<div class="card-body">
#foreach($supervisor as $data)
<form id="supProfileForm" type="POST" action="{{url('supervisor/profile',$data->user_id)}}"
enctype="multipart/form-data">
#csrf
<div>
<label for="name"> Name : </label>
<input id="name" disabled value="{{$data->name}}">
</div>
<div>
<label for="email">Email : </label>
<input id="email" disabled value="{{$data->email}}">
</div>
<div>
<label class="inline" for="expertise">Expertise : </label>
<input type="text" id="expertise" name="expertise" value="{{$data->expertise}}">
</div>
<div>
<button type="submit" class="btn btn-primary">Update</button>
</div>
</form>
</div>
</div>
#endforeach
Then, this is my function in Controller: I just wanna update the expertise.
public function supProfileUpdate(Supervisor $supervisor)
{
$this->validate(request(),[
'expertise'=>'required',
]);
$supervisor->expertise = request('expertise');
$supervisor->save();
return redirect()->back();
}
I think the problem caused by
Route::post('supervisor/profile/{id}','UserController#supProfileUpdate');
where the Supervisor model primary key is id while you trying to get it using the user_id key.
So I will suggest that you either implement getRouteKeyName in Supervisor model as follows
/**
* Get the route key for the model.
*
* #return string
*/
public function getRouteKeyName()
{
return 'user_id';
}
or by using the id when sending it to the form
<form id="supProfileForm" type="POST" action="{{url('supervisor/profile',$data->id)}}"
I used method store from a controller and data doesn't store in my database.
I want to insert NOM_ARTICLE, PHOTO_ARTICLE, TYPE, DESCRIPTION_ARTICLE in three tables from a form in native HTML, but I don't know what action to do.
I am using ArticleController where there is a method store and three table models.
public function create()
{
return view('addarticle');
}
public function store(Request $request)
{
$article = new article;
$article->NOM_ARTICLE = $request->NOM_ARTICLE;
$article->LABEL_TYPE = $request->LABEL_TYPE;
$article->PHOTO_ARTICLE = $request->PHOTO_ARTICLE;
$article->DESCRIPTION_ARTICLE = $request->DESCRIPTION_ARTICLE;
$article->save();
return redirect()->route('addarticle');
}
Here are my tables from database:
article ('ID_ARTICLE','NOM_ARTICLE','ID_TYPE,'DESCRIPTION_ARTICLE')
photo_articles('ID_PHOTO','ID_ARTICLE','PHOTO_ARTICLE')
type('ID_TYPE','TYPE')
My HTML form:
<form method="post" action="" class="contact_form text-center" id="contact_form">
<div class="">
<div class="col-lg-4">
<input type="text" class="contact_input" name="NOM_ARTICLE" placeholder="Nom d'article" required="required">
</div>
<br/>
<div class="col-lg-4">
<input type="text" class="contact_input" name="ID_TYPE" placeholder="Type d'article" required="required">
</div>
<div>
<input type="hidden" name="MAX_FILE_SIZE" value="250000" />
<input type="file" class="contact_input" name="PHOTO_ARTICLE" placeholder="Capture de votre article" name="fic" size=50 required="required" />
<!-- <input type="submit" value="Envoyer" /> -->
</div>
</div>
<textarea class="contact_textarea contact_input" name="DESCRIPTION_ARTICLE"placeholder="Description" required="required"></textarea>
<button class="contact_button" type="submit">Valider!</button>
</form>
And I have my route in web.php:
Route::resource('addarticle','ArticleController');
And this is my Article model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class article extends Model {
public $table = 'article';
public $primaryKey ='ID_ARTICLE';
return $this->belongsTo('photo_articles');
}
After I click on the submit button it shows a URL like this: http://localhost/testprojet/public/addarticle?NOM_ARTICLE=test&ID_TYPE=book&MAX_FILE_SIZE=250000&PHOTO_ARTICLE=villle+icon.jpg&DESCRIPTION_ARTICLE=ss
And redirecting to my view addarticle but nothing gets added in the database.
Yaa You clearly missed csrf token to pass with the form. If you want to bubmit any form in Laravel, you should pass csrf token.
use #csrf and blade will pass the unique token to the form submit
https://laravel.com/docs/5.8/csrf
Example:
<form method="post" action="" class="contact_form text-center" id="contact_form">
#csrf
<div class="">
<div class="col-lg-4">
<input type="text" class="contact_input" name="NOM_ARTICLE" placeholder="Nom d'article" required="required">
</div>
<br/>
<div class="col-lg-4">
<input type="text" class="contact_input" name="ID_TYPE" placeholder="Type d'article" required="required">
</div>
<div>
<input type="hidden" name="MAX_FILE_SIZE" value="250000"/>
<input type="file" class="contact_input" name="PHOTO_ARTICLE" placeholder="Capture de votre article" name="fic" size=50 required="required"/>
<!-- <input type="submit" value="Envoyer" /> -->
</div>
</div>
<textarea class="contact_textarea contact_input" name="DESCRIPTION_ARTICLE" placeholder="Description"
required="required"></textarea>
<button class="contact_button" type="submit">Valider!</button>
</form>
What I see from your form is that you didn't put any action in it, you just left it empty:
<form method="post" action="" class="contact_form text-center" id="contact_form">
You should be putting in a route to your store method in your controller, so that when the form is submitted, the data will be passed into your controller.
Also, providing a link to your localhost isn't helping us to understand your problem as we can't access to it. A solid screenshot will do.
You should put the action according to the resource pattern.
Your web.php
Route::resource('articles','ArticleController');
Your form:
<form method="post" action="{{route('articles.store')}}" class="contact_form text-center" id="contact_form">
#csrf
You should include the #csrf blade directive, as well.
In you controller:
public function store(Request $request)
{
$article = new Article();
$article->NOM_ARTICLE = $request->NOM_ARTICLE;
$article->DESCRIPTION_ARTICLE = $request->DESCRIPTION_ARTICLE;
$article->save();
return redirect()->route('addarticle');
}
You're adding the PHOTO_ARTICLE to the article but you dont have this column in your articles table as you said, so it dont make sense to use the line below:
$article->PHOTO_ARTICLE = $request->PHOTO_ARTICLE;
Also, you're not receiving any LABEL_TYPE from your request, and you dont have this column in your articles table too. So you must remove this line:
$article->LABEL_TYPE = $request->LABEL_TYPE;
Try this: I hope it will help you.
Controller
public function store(Request $request)
{
$articleObj = new article;
$articleObj->add($request);
//second table
$employerObj = new Employer();
$employerObj->add($request);
}
Create three model according to your datatables and paste this code for all.
function add($request)
{
$this->NOM_ARTICLE = $request->NOM_ARTICLE;
$this->LABEL_TYPE = $request->LABEL_TYPE;
$this->PHOTO_ARTICLE = $request->PHOTO_ARTICLE;
$this->PHOTO_ARTICLE = $request->PHOTO_ARTICLE;
$this->save();
return $this->id;
}
This my current POST route.
Route::post('/eAPI', 'ApiController#eAPI');
I wanted to make it like
Route::post('/q={$number}', 'ApiController#eAPI');
But in my form.
<form action="{{url('/eAPI')}}" method="post" id="search">
<div class="form-group">
<label for="number" class="col-md-4 control-label">Telephone Number to search :</label>
<div class="col-md-6">
<input class="form-control" id="number" name="number" placeholder="Phone (eg. 5551234567)" required>
</div>
</div>
<div class="col-md-2">
<input type="submit" name="name" value="Find" class="btn btn-success">
</div>
</form>
Now, I want to put a variable in this part, something like this.
<form action="{{url('/?q=$number')}}" method="post" id="search">
In post request you should do it like this:
Route::post('/eAPI/{q}', 'ApiController#eAPI')->name('my_route');
And in HTML Form:
<form action="{{ route('my_route', ['q' => '4']) }}" method="post" id="search">
</form>
And inside controller you can retrieve it as:
Class ApiController {
public function eAPI($q) {
// Use $q here ...
}
}
Hope this helps!
This works for me [method post and url has ?q=someValue] :
public function eApi(Request $request){
$q = $request['q'];
}
This code will get all params in post and get method
$request->all()
Hope it helps!
I never did and will never do this with post requests, but it works with get requests:
$q = request()->q;
And you don't need to add this to the route: q={$number}, just add parameters to url: ?q=value1&s=value2&c=value3
I wish to clarify the steps to get a new form properly submitted to my database using Laravel 5.2 and Bootstrap 3.
I have the login/register pages set up properly using Laravel's defaults, and they work fine. I now want to create a user profile page accessible to authenticated users. I am using one row in the database for all of their user info. Some fields were filled in during registration, and now I want them to have access to additional fields (while restricting access to certain registration fields like user name).
In the example code below, there are fields to upload a personal photo, enter a first name, and enter a last name. (None of these were done during registration.)
What I have already done (all code is below):
Create the view profile.blade.php
Create a controller profileController.php
Update routes.php in the controller directory.
A note:
When I try to submit the form as it appears below, I get, Type error: Argument 1 passed to App\Http\Controllers\ProfileController::update() must be of the type array, none given.
What are the next steps required to get this page working properly?
profile.blade.php:
#extends('layouts.app')
#section('content')
<div class="container" style="padding-top: 30px;">
<h1 class="page-header">User Profile</h1>
<div class="row">
<!-- left column -->
<div class="col-md-4 col-sm-6 col-xs-12">
<div class="text-center">
<i class="fa fa-user fa-5x"></i>
<h6>Please upload a photo...</h6>
<input type="file" class="text-center center-block well well-sm">
</div>
</div>
<!-- edit form column -->
<div class="col-md-8 col-sm-6 col-xs-12 personal-info">
<form class="form-horizontal" role="form" method="POST" action="{{ url('/profile') }}">
{!! csrf_field() !!}
<div class="form-group">
<label class="col-lg-3 control-label">First name:</label>
<div class="col-lg-8">
<input class="form-control" value="<?php echo Auth::user()->firstname; ?>" id="firstname" name="firstname" placeholder="First..." type="text">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Last name:</label>
<div class="col-lg-8">
<input class="form-control" value="<?php echo Auth::user()->lastname; ?>" id="lastname" name="lastname" placeholder="Last..." type="text">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<button type="submit" class="btn btn-primary">
<i class="fa fa-btn fa-user"></i>Submit
</button>
<span></span>
<input class="btn btn-default" value="Cancel" type="reset">
</div>
</div>
</form>
</div>
</div>
</div>
#endsection
profileController.php:
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
class ProfileController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('profile');
}
protected function update(array $data)
{
return User::update([
'firstname' => $data['firstname'],
'lastname' => $data['lastname'],
]);
}
}
And I added the following in the routes middleware:
Route::get('/profile', 'ProfileController#index');
Route::post('/profile', 'ProfileController#update');
It's a protocol mismatch, since you're POSTing your form. You need to change your route to
route::post('/profile', 'ProfileController#index');
Using a validator is a great idea, since it will make sure that your input is exactly what you need it to be, and all required fields are filled out.
Your update function should look something like this:
public function update(Request $request)
{
$first_name = $request->input('firstname');
$last_name = $request->input('lastname');
$id = Auth::user()->id;
$user = \App\User::find($id);
$user->firstname = $first_name;
$user->lastname = $last_name;
$user->save();
return view('profile');
// Sanitize, validate, before you do ANYTHING with update
// Instead of returning the update result, you can instead show another view or forward them to another page.
}
i just wanted to run html source code but when i enter html source
code to my text area and run it it displays only htmlcode as a string
but i wanted to run that code...............................
View page
#extends('layouts.theme')
#section('content')
<div class="col-sm-8 col-sm-offset-2" style="border: 1px solid #ccc;">
<form class="form-horizontal login-form" method="post" name="loginFrm" id="loginFrm">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label for="f" class="col-sm-3 control-label"></label>
<div class="col-sm-12">
<textarea name="txtinput" placeholder="Paste your html code here" class="col-sm-12" rows="20"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<button type="submit" name="subBtn" class="btn btn-info pull-right">Format and Analyze</button>
</div>
</div>
</form>
</div>
<div style="clear:both"></div>
<div>
#if(count($htmlpagedata)>0)
{{ htmlentities($htmlpagedata) }}
#endif
</div>
#stop
Controller
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
class JHtmlParsing extends Controller {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
return view('input');
}
/**
* Parse HTML Page.
*
* #return Response
*/
public function parsehtml(Request $request)
{
$data=$request->get('txtinput');
return view('input')->with('htmlpagedata',$data);
}
}
By default, Blade {{ }} statements are automatically sent through PHP's htmlentities function to prevent XSS attacks.
If you do not want your data to be escaped, you may use the following syntax:
{!! $variable !!}
you are using blade template so you need to extend .blade.
for example your file name is login then it should be
login.blade.php