I want to get the value of this input.
<input type="text" name="txtEmployeeNo" value='{{ $employee->employee_no }}'>
Its value is 53210. How can I get that in my controller?
I currently have this on my controller.
$employeeNum = $request->input('txtEmployeeNo');
$employeeSched = Schedule::where(['employee_no'=>$employeeNum])->get();
return view('admin.employeemaintenance.createSchedule',compact(,'employeeSched'));
The problem is when I open and see if it is fetched nothing is showing. I cannot get the input.
Try this, It should must work.
$employeeNum = (isset($request['txtEmployeeNo'])) ? $request['txtEmployeeNo'] : 0;
$employeeSched = Schedule::where(['employee_no'=>$employeeNum])->get();
return view('admin.employeemaintenance.createSchedule',$employeeSched);
In your controller insert this line after opening your function:
dd($request->all);
It will show you everything that has been posted through your form with values. If you get your 'txtEmployeeNo' without value, it means something went wrong when you insterted it in your input.
Check with dev tools if that specific input has any value.
If your input has the value you mentioned and your $request->all() still shows an empty value for your "txtEmployeeNo", then the error is in the HTML/Blade file.
Make sure you create the form correctly
Make sure your input's name equals with the request you are trying to receive in your controller.
If you get null as the value of the $request, that could mean, in your Blade file, the input also has it's value as null.
Try to manually insert a value like <input type="text" name="txtEmployeeNo" value="2"> and see if you get that in your controller. If you do, then the query in your input is wrong.
That's all I could think of without provided Blade and Controller code.
Try this:
$employeeNum = $request->input('txtEmployeeNo');
$employeeSched = Schedule::where('employee_no', $employeeNum)->get();
return view('admin.employeemaintenance.createSchedule',compact('employeeSched'));
well, here is an edit to this answer with the steps needed:
in your routes:
Route::post('yourRouteName','yourController#nameOfFunctionInController')->name('TheNameOfTheRoute');
In your controller:
public function nameOfFunction(Request $request) {
$employeeNum = $request->input('txtEmployeeNo');
$employeeSched = Schedule::where('employee_no', $employeeNum)->get();
return view('admin.employeemaintenance.createSchedule',compact('employeeSched'));
}
And that's it basically.
Related
I'm getting this error
My Controller:
In controller after student save add following
$success='student data updated';
return view('your_view_name',compact('success','student'));
if your view name is inside resources/views/student.blade.php you should write like this
return view('student',compact('success','student'));
You forgot to pass $student variable for view, You need to pass it like:
return view()->route('student.index',compact('student'))->with('success','Student data updated.');
// OR
return view()->route('student.index',['student'=>$student])->with('success','Student data updated.');
Details Here: https://laravel.com/docs/5.6/routing
You must return the student variable to blade file using compact functionality
return view('student.index', compact('student'))->with('success','Student data updated.');
and one more thing is you can Use HTML entities to escape curly braces to set the value of input. like
<input type="text" class="form-control" name="first_name" value="{{$student->first_name}}">
I want to get current page name from url using laravel blade, as I want to use it for dynamic manipulation and put it inside hidden value.
If the URL is admin/coding/colors, I want to get only colors page name.
Is that possible?
If you want it to respond to any uri, irrespective of how many segments are in the url, try:
{{substr(strrchr(url()->current(),"/"),1)}}
This will always get the last segment of the request
Of course, try this:
use \Illuminate\Support\Facades\Request;
{{Request::segment(3)}}
{str_after(url()->current(), 'http://admin/coding/')}}
You should obtain the value in the controller and give it as a variable to your view.
You can do that with the segments method of the Request.
$request->segments()[count($request->segments()) -1]
Your controller should look something like this:
public function someFunctionName(Request $request)
{
$last_url_segment = $request->segments()[count($request->segments()) -1];
return view('view.name', ['last_url_segment', $last_url_segment]);
}
Then, in your view, you can use the variable.
<input type="hidden" name="url" value="{{ $last_url_segment }}">
fHello, for example, i have simple input field (page index.php)
<input type="text" name="name" value="{{Request::old('name')}}">
In controller
$this->validate($request, ['name' => 'required']);
After this, i want make some check without Laravels rules. For example
if($request['name'] != 'Adam') { return view('index.php'); }
But after redirect, Request::old is empty. How to redirect to index.php and save old inputs and use Request::old, or its impossible? Thank you.
PS its example, i know that Laravel has special rules for check inputs value
Old question, but for future reference, you can return the input to a view by flashing the request input just beforehand.
i.e.
session()->flashInput($request->input());
return view('index.php');
then in your view you can use the helper
{{ old('name') }}
or
{{Request::old('name')}}
In Laravel 8.x, you can simply use $request->flash();
Docs: https://laravel.com/docs/8.x/requests#flashing-input-to-the-session
You can use back() instead if any url. These function helps you in any case to be able to return to previous page without writing route.
return back()->withInput();
To add the input to your request try adding:
return view('index.php')->withInput();
I'm new to codeigniter so sorry for the simple question.
I want to run formvalidator for my ajax process. How can I return the valdiation errors as JSON? Is there a better way to do this?
Thanks
you haven't really stated what you are doing. i am going to assume that you have a form that you want to save data in, using ajax, so that you don't have any of those pesky save/submit buttons. also, i am guessing that you have some sort of .change() handler that sends the form element to the ajax handler as a post variable as a name/value pair.
the problem you will run into, is that when you run the form validator on your data it will always fail. because the form validator needs all of the fields for that form, and you will only send one piece of data at a time.
normally in the code igniter example code you check to see if the 'run' method passess or not. in your case it doesn't really matter because it will always fail, so don't bother checking. here is a snippet of some example code
$this->form_validation->run('form'); // it is likely that the form won't validate, but thats ok.
$validation_error=form_error($field_name);
if($validation_error){
$feedback = 'Field <strong>NOT</strong> saved.';
}else{
// no errors, we can save.
$this->my_model->set_field($id,$field_name,$field_value);
$validation_error=' '; // this is set so if the field was initially not validated, and it is later, the validation message goes away.
$validation_element = '#'.$field_name;
$feedback = 'Field saved.';
}
....
echo json_encode(array('feedback'=>$feedback,'validation_element'=>'#'.$field_name,'validation_error'=>$validation_error));
break;
in my snippet, a json object is returned to the ajax post. in the jquery ajax post,the success handler has this snippet of code.
try{
var json = $.parseJSON(data);
if(typeof(json.container)==='string'){
var container=json.container;
}
if(typeof(json.html)==='string'){
var con = $(container);
$(con).html(json.html);
}
if(typeof(json.feedback)==='string'){
display_feedback(json.feedback);}
if(typeof(json.validation_element) ==='string'){
// assumes that if a validation_element object is returned, it has a paired validation_error element.
$(json.validation_element).closest('.col_b').nextAll('.validation_error').first().html(json.validation_error);
}
}
catch(err){
display_feedback(err);
}
in my form, i have a three column format, where:
<div class='col_a'>label</div>
<div class='col_b' >input, id = field_name </div>
<div class='col_c validation_error'>validation text</div>
hopefully that makes some sense to you.
I have a form that I need to validate but I can't figure out how to code the controller so that it works correctly the first time the page is displayed. Here is my code (simplified):
function index()
$data['somedata'] = $this->input->post('somedata');
$this->form_validation->set_rules('event', 'Event', 'trim|required|alpha_numeric');
... more set_rules ...
if($this->form_validation->run() == FALSE)
{
// Hasn't been run or there are validation errors
$this->load->view('eventview', $data);
}
else
{
// Process the event
}
}
The problem is that form_validation->run() is never FALSE because the $_POST array contains data from a previous form that is used by this second form. At the very beginning of the form_validation->run() function is the following code:
// Do we even have any data to process? Mm?
if (count($_POST) == 0)
{
return FALSE;
}
Since $_POST data exists the count is always greater than zero which results in the validation to be processed on initial page load.
Any suggestions as to how I might work around this?
I would suggest that you save the data from your first form in sessions, then make a redirect to your second form instead of going directly to the next one, with your post data.
This would also be more flexible if you need to reuse any of the submitted data.
In the first form set a hidden input
<input type="hidden" name="form1" value="form1" />
Then in your controller check if the field is set, if so store the current post array, and then unset it.
if(isset($_POST['form1'])){
$old_post = $_POST;
unset($_POST);
}
as I assume you are accessing the $_POST array in your view, instead pass `$old_post` through and use that,
e.g. (isset($old_post) ? $old_post['field_name'] : set_value('field_name))
Good luck.