Problem with displaying content entered in the form in laravel.
What code to add to display the value entered in the form?
--web.php
Route::get('/show-name', ['uses' => 'NameController#show-name', 'middleware' => 'auth']);
--
NameContoller.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class NameController extends Controller
{
public function show-name()
{
return view('show-name');
}
}
--
show-name.blade.php
<?php
print_r($_POST);
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" class="">
<div class="required field">
<label>Name</label>
<input type="text" name="email" id="name">
</div>
<input type="submit" class="ui primary button" id="send" name="send" value="Send"></input>
</form>
Message after using the button - MethodNotAllowedHttpException.
I will be grateful for your help.
You're sending a POST request, not a GET request.
Route::post('/show-name', [
'uses' => 'NameController#show-name',
'middleware' => 'auth'
]);
The documentation tells you how to get all the data
https://laravel.com/docs/5.8/requests#retrieving-input
The documentation tells you have to pass data to a view https://laravel.com/docs/5.8/views#passing-data-to-views
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class NameController extends Controller
{
public function show-name(Request $request)
{
$input = $request->all();
return view('show-name')->with('data', $input);
}
}
The documentation tells you how to access data passed into a view https://laravel.com/docs/5.8/blade#displaying-data
#php
echo print_r($data);
#endphp
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" class="">
<div class="required field">
<label>Name</label>
<input type="text" name="email" id="name">
</div>
<input type="submit" class="ui primary button" id="send" name="send" value="Send"></input>
</form>
Further more, the form won't work without CSFR https://laravel.com/docs/5.8/blade#forms
Inputs are self closing, meaning there is no need for </input>
Further more, there is no need for <?php echo $_SERVER['PHP_SELF']; ?> because with Laravel, you can specifically define the name of the route.
<form method="post" action="/show-name" class="">
Watch some basic tutorials on Laravel because you're going about this all incorrectly.
Related
My View:
<form action="/AddJuiceForm" method="POST">
#csrf;
<div class="form-group">
<label for="FruitName">Fruit Name : </label>
<input type="text"
class="form-control"
placeholder="Enter the Fruit Name"
name="fruitname">
</div>
<div class="form-group">
<label for="JuiceName">Juice Name : </label>
<input type="text"
class="form-control"
placeholder="Enter the Juice Name"
name="juicename">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
My Routes/web.php:
Route::post('AddJuiceForm','JuiceController#insertjuice ');
My Controller:
<?php
namespace App\Http\Controllers;
use App\JuiceModel;
use Illuminate\Http\Request;
class JuiceController extends Controller
{
public function insertjuice()
{
dd(request()->all);
}
}
Result: I am getting 'null' as output
Help me where I am going wrong?
Inject the Illuminate\Http\Request object into the insertjuice method and then call the all() method on the variable within your JuiceController class:
public function insertjuice(Request $request)
{
dd($request->all());
}
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;
}
I have the following external form:
<form method="POST" action="http://infused.local/leads/post">
<div class="form-group">
<label>first_name</label>
<input type="text" name="first_name" class="form-control">
</div>
<div class="form-group">
<label>last_name</label>
<input type="text" name="last_name" class="form-control">
</div>
<div class="form-group">
<label>email</label>
<input type="text" name="email" class="form-control">
</div>
<div class="form-group">
<label>postal_code</label>
<input type="text" name="postal_code" class="form-control">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
Which points to the following controller method:
public function post()
{
$this->validate(request(), [
'email' => 'required|email',
]);
echo 'hello';
}
Via this route:
Route::post('leads/post', 'LeadController#post');
I've disabled CSRF protection for the form route.
When I submit the form, I get "Failed to load response data" from
Chrome.
When I remove the $this->validate call, I get "hello".
Why is this not working?
You need to return JSON and you can use the helper function:
response()->json([
'message' => 'hello'
]);
Then in your JS:
console.log(response.message);
OK apparently the validate method just redirects you back to the previous page if there are errors, unless the response is expected to be JSON. Ended up using the Validator facade instead.
Edit: Thanks for the downvotes. This website is cancer.
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.
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