I'm working on Laravel project and I want to send my input values from the FORM from one page (the inscription page) to a pdf page (which I want the user to be able to download). I couldn't find a way to send them from that page to the other
input:all
using the $request in the controller
<div class="fieldgroup">
<input type="text" style="color:0B0C51" onclick="submitform2()" name="cin" id="cin"
placeholder="NĀ°CIN" maxlength="8" class="required"></i><br>
</div>
<div class="fieldgroup">
<input type="text" style="color:0B0C51" v-model="prenom" name="prenom" id="prenom"
placeholder="Prenom" class="required"><br>
</div>
<div class="fieldgroup">
<input type="text" style="color:0B0C51" v-model="nom" onclick="submitform2()" name="nom"
id="nom" placeholder="Nom" class="required"><br>
</div>
<div class="fieldgroup">
<input type="mail" style="color:0B0C51" name="email" id="email" placeholder="Email"
class="required" />
</div>
ViewController :
class ViewController extends Controller
{
public function generatePDF(Request $request){
$request=this.
$data="form";
$pdf= PDF::loadView('pdf',compact('data'));
return $pdf->download('Terms.pdf');
}
}
web.php:
Route::get('/pdf','ViewController#generatePDF');
inscriController:
public function store(Request $request)
{
$cin = $request->input('cin');
$data = array(['cin'=>$cin ]);
DB::table('form')->insert($data);
return redirect('/pdf')->withInput();
This returns an empty form page without the input values
It may be easier to load the pdf right in the store method of inscriController. As it is, you are redirecting and passing no data into the pdf generator / view. You set $request as the controller ($this), which has none of the form data, and then you pass a single word 'form' as your data-set into the PDF generator. I don't think this will work the way you wish - I assume the pdf view is looking for specific variables (not 'form'), and thus it is failing because those vars are missing. IE you'd want to pull those vars like $name = $request->get('name') and pass whatever pdf.blade.php view needs from your $request var.
If you don't want to do it all in the store function, perhaps pass it to a method in the same inscriController? This way you can easily push the actual request object into that method and pull the fields you need for the PDF view.
use $input = $request->all(); for getting the data and in same function instead of redirect u can just return view for your /pdf route.
like return view('yourpath.pdf', compact('input'));
Related
Hello i'm new in laravel. My application has a search bar component in almost every view.
The user types there the ID of the client, makes a query to db and compares the user's typed ID with the DB client ID.
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
class SearchController extends Controller
{
function get_kcli(Request $request) {
$id = $request->input('kcli');
$current_page = $request->input('currentPage') . '.index';
$data = DB::connection('oracle')->table('CLIENTS')->where('KCLI', $id)->get();
return view($current_page, compact('data'));
}
}
Web.php
Route::post('/search', [App\Http\Controllers\SearchController::class, 'get_kcli'])->name('search');
Form search component view:
<nav class="navbar navbar-light">
<form method="POST" class="form-inline position-relative" action="{{ route('/search') }}">
#csrf
#method('POST')
<input class="form-control shadow-none" name="kcli" id="kcli" type="number" placeholder="Codice..." aria-label="Search">
<input type="text" name="currentPage" value="{{ Route::currentRouteName() }}" hidden>
<button type="submit" class="btn btn-light search-btn"><i class="fas fa-search"></i></button>
</form>
Anyone knows the best practice to store that data request globally an mantain it for every view that need to exctract it?
Example : I search for the client ID '5', the search controller makes a db query with the passed ID, then compacts data. That data has to be stored globally for get the results and post it in other views, without searching again on every view switch(the typed ID '5' has to remain in the search field on every view switch).
To print client_id you can store that "id" in session and print from the session by this way your search value keep remains and to get data in all views there is the concept View::share you can register that in AppServiceProvider please go through this link
https://laravel.com/docs/9.x/views#sharing-data-with-all-views
When trying to implement https://laravel-livewire.com/docs/2.x/input-validation - Real-time Validation I noticed a problem. Let's assume that we have 3 inputs with real time validation, if I type into first one some value and click tab to go to next input, again i enter the value and go to 3 input, the second and/or third input will glitch because there are 3 request(in case you use https://laravel-livewire.com/docs/2.x/properties#lazy-updating) or even more request if you don't use it. And because of this the value from second and/or third input disappear until all request hit the server. This can be notices with normal internet connection, but with slow internet it's even more obvious.
Steps to reproduce:
Create a livewire component with this content
namespace App\Http\Livewire;
use Livewire\Component;
class ShowPosts extends Component
{
protected $rules = [
'field1' => 'required',
'field2' => 'required',
'field3' => 'required',
];
public $field1;
public $field2;
public $field3;
public function updated($propertyName)
{
$this->validateOnly($propertyName, $this->rules);
}
public function render()
{
return view('livewire.show-posts');
}
}
And this view
<div>
<form wire:submit.prevent="submit">
<div class="form-group mb-3 col-12">
<input class="form-control" type="text" wire:model="field1">
</div>
<div class="form-group mb-3 col-12">
<input class="form-control" type="text" wire:model="field2">
</div>
<div class="form-group mb-3 col-12">
<input class="form-control" type="text" wire:model="field3">
</div>
</form>
</div>
Include it in a blade view with #livewire('show-posts')
In chrome dev tools go to network->online dropdown->select slow 3g connection
Try to enter value for each input and go to next with tab, you will notice that value from previous input disappear or change to the last value that was before modification
What can be done to avoid this disappearing/changing of the input value?
I'm validating laravel forms and old input does not preserve after validation fail.
I'm using laravel 5.8 form request validation and html input fields are filled with default values :
public function store(ProyectoRequest $request){
$input = $request->validated();
DB::transaction(function () use($input) {
$proyecto = new Proyecto();
$proyecto->fill($input);
$proyecto->save();
});
return redirect(route('proyectos.index'));
}
<div class="form-group">
<label for="nombre">Nombre</label>
<input type="text" class="form-control input-lg" value="{{$proyecto_nombre}}" name="nombre">
</div>
When the form is validated the page reloads with the default values. I expect that input fields values are preserved. That is what I have understood from the documentation.
I am looking for the docs and there is not written if this is the normal behaviour or not.
Anyway you can do yourself with this code:
<div class="form-group">
<label for="nombre">Nombre</label>
<input type="text" class="form-control input-lg" value="{{ old('proyecto_nombre', $proyecto_nombre) }}" name="nombre">
</div>
In laravel the old function retrieves input fields.
I need to get a value of input to use below, how to do that?
I tried to like this but error says
Undefined variable: name
<div class="col-md-10 col-md-offset-1">
<input id="name" type="text" name="name" />
</div>
<div class="col-md-10 col-md-offset-1">
#php
$nameValue=$_GET['name'];
#endphp
<input id="name2" type="text" name="name2" value="{{$nameValue}}" />
</div>
$nameValue=Request::input('name')
From the blade template you can access the request parameters with the Request facade, you can also print it directly:
{{Request::input('name')}}
In latest versions you can also use:
{{request()->input('name')}}
You have to be aware that your input-values (here "name") ist only available after submitting the form.
If you want to access the form-values before submitting you should take a look at VueJS or any other frontend-framework (React, Angular). Or simply use jQuery.
Therefor you have to use JavaScript if you want to use the input-value before submitting.
Like the others said in the comments, you can access your form-values within your controller and then pass it to your view.
For example (from the documentation):
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function formSubmit(Request $request)
{
$name = $request->input('name');
return view('form', ['name' => $name])
}
}
Now you can use the value within your view:
<input id="name2" type="text" name="name2" value="{{$name}}">
Another possibility would be to "by-pass" your controller and return your view directly from your routes.php:
Route::get('/form-submit', function(){
return view('form');
});
But I'm not sure if this is working and you could access $_GET/$_PSOT directly without using Laravels Request.
You can get inputs array from Request class:
Request::all()['your_input']
Also you can check if that input you want is exists not:
#isset(Request::all()['your_input'])
{{-- your input existed --}}
#else
{{-- your input does not existed --}}
#endisset
I have form in my nav template in codeigniter view
<form action="<?php echo base_url()?>account/login/" class="navbar-form navbar-left" method="post" accept-charset="utf-8">
<div class="form-group">
<input type="text" name="adres" class="form-control" placeholder="Email">
<input type="password" name="pass" class="form-control" placeholder="Password">
</div>
<button type="submit" class="btn btn-default">Login</button>
</form>
In my controller method i can echo $_POST['adres'] and its fine, but when i try with codeigniter helper $this->input->post('adres') its empty . Whats wrong ?
I'm using input->post in my registration form and its working fine.
General Advice of using the forms in CodeIgnitor
Step 1: Try to use the native form method that are available in CI.
Syntax for Form Open:
form_open('[controller]/[action]')
Hence below is the sample example of how to open up the form based on the controller that we have created.
<?php echo form_open('todos/update_completed'); ?>
<?php echo form_close(); ?>
Where todos/update_completed means
todos - Controller Name
update_completed - Method name in that Controller.
Step 2: Load up the form elements during the auto load itself or you can load up the form elements in the Construct function itself.
Loading the form attributes via the helper in the construct function.
$this->load->helper('form');
Below is the example of how to call it.
function __construct()
{
parent::__construct();
$this->load->helper('form');
}
Step 3: And in the Controller or in the model you have to get the Input that has been posted like this below.
$this->input->post('title')
$this->input->post('username')
These are all the General Checks that has to be made while using the form elements in CodeIgnitor.
Yes, problem solved. I was using curly brace and i should use a normal one. My bad
Have you used helper class in constructor $this->load->helper('form'); or in method .
You can load helper function in your controller like this
function __construct()
{
$this->load->helper('form');
}