How to change input attribute "values" in codeigniter on submit - codeigniter

This is my code snippet
<input type="hidden" value="<?php echo $details[0]->invoice_id;?>" name="invoice_id">
on submit I need to change the value by increment of 1 and pass it to controller $invoice_id=$this->input->post('invoice_id',true);.
This code is just updating with same invoice_id in database table.
Please help me to write a code that will increment value on each submit.

you can't use auto increment in database?
if not, use something like this:
<input type="hidden" value="<?php echo $details[0]->invoice_id;?>" name="invoice_id">
on controller use:
$invoice_id = $this->input->post('invoice_id');
$invoice_id = $invoice_id++;
if it receives 1 on first line, will be 2 on second line... You just need to use the $invoice_id anywhere you need.

Thank You all for your precious time. I got the result finally.
In model I wrote a function to extract the invoice_id
function get_details()
{
$this->db->trans_start();
$query = $this->db->query("SELECT * from `invoice_details` ORDER BY invoice_id DESC");
$this->db->trans_complete();
if($query->num_rows()>=1)
return $query->result()[0]->invoice_id;
}
and in the controller I extract this invoice_id through
$invoice_id=$this->invoice_m->get_details();
and pass this value.

Related

How to get array value that get from checkbox input in Laravel's controller?

I want to send 'Date' array from a page to another page through checkbox input for display the data and get more data from database.
I try to dd() the array from input, it's still normal but the data just only show 1 value when I use 'foreach' loop. How do I do?
<input name="isInvoices[]" type="checkbox" value="{{ $date }}">
$invoices = $request->input('isInvoices');
// dd($invoice); It's show array of date
foreach($invoices as $invoice) {
dd($invoice); //It's just only 1 value
}
I expected the output to show all value in the array, but the actual output is show 1 value.
dd means dump and die. Which after the first iteration stops, that's why you see only one item. Try this:
foreach($invoices as $invoice) {
dump($invoice);
}
die;
before such would function, you have to have series of checkbox like:
this is display series of date values on checkbox. so when you select any dates and submit
foreach($datevalue as $date){
<input name="isInvoices[]" type="checkbox" value="{{ $date }}">
}
//this gets the date value
$invoices = $request->input('isInvoices');
//this will display all the selected checkboxes
foreach($invoices as $invoice) {
dd($invoice);
}
dd() – stands for “Dump and Die”, and it means as soon as loop gets to this command, it will stop executing the program, in this case in first interation. To display all data in foreach loop, use echo or print_r():
foreach($invoices as $invoice) {
echo $invoice;
}
This means that the program will print $invoice for each iteration. Then you can access your data like this:
$invoice['value']
You can read more about different types of echoing here.

Get Input Data Value in Controller in Laravel

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.

Laravel Undefined variable Error

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}}">

Laravel 4: Form::macro with Form::model

Is it possible to use a custom Form::macro() with the Form::model() feature?
When I tried it at first glance, I could not get the model data to be passed to the macro method.
Only Form functions like Form::text will look for the form model automatically. Inside your macro, you could do this a couple of ways. Easiest would be to use Form::getValueAttribute($name). For example:
Form::macro('myField', function() {
$value = Form::getValueAttribute('username');
return "<input type='text' name='username' value=$value >";
});
And then you'd use it in the blade template like this:
<?php
$user = new User;
$user->username = "bob";
echo Form::model($user);
echo Form::myField();
echo Form::close();
?>
You can find all of the available form functions in the source code here: https://github.com/laravel/framework/blob/master/src/Illuminate/Html/FormBuilder.php

Codeigniter: How to build an edit form that uses form validation and re-population?

I have a simple form in codeigniter that I wish to use for the editing or records.
I am at the stage where my form is displayed and the values entered into the corresponding input boxes.
This is done by simply setting the values of said boxes to whatever they need to be in the view:
<input type="text" value="<?php echo $article['short_desc'];?>" name="short_desc" />
But, if I wish to use form_validation in codeigniter then I have to add thos code to my mark-up:
<input value="<?php echo set_value('short_desc')?>" type="text" name="short_desc" />
So not the value can be set with the set_value function should it need to be repopulated on error from the post data.
Is there a way to combine the two so that my edit form can show the values to be edited but also re-populate?
Thanks
set_value() can actually take a second argument for a default value if there is nothing to repopulate (At least looking at CI versions 1.7.1 and 1.7.2). See the following from the Form_validation.php library (line 710):
/**
* Get the value from a form
*
* Permits you to repopulate a form field with the value it was submitted
* with, or, if that value doesn't exist, with the default
*
* #access public
* #param string the field name
* #param string
* #return void
*/
function set_value($field = '', $default = '')
{
if ( ! isset($this->_field_data[$field]))
{
return $default;
}
return $this->_field_data[$field]['postdata'];
}
So, with that in mind you should be able to simply pass your default value to set_value like this:
<input value="<?php echo set_value('short_desc', $article['short_desc'])?>" type="text" name="short_desc" />
If there is no value to repopulate, set_value() will default to $article['short_desc']
Hope that helps.

Resources