I having a problem to validate multiple row data in Codeigniter by using form validation. Below code are ok to validate the 1st row of data. The subsequent row data i added via javascript was not able to validate. After i clicked submit, the next row that i disappeared. Anyone can help to this?
Appreciate and thanks in advance for any help.
HTML
<!-- Form -->
<?php echo form_open(base_url('air'), array('class'=> 'form-horizontal','id'=> 'calc_form')) ?>
<table>
<tbody id="item-row">
<tr>
<td>1</td>
<td><input class="form-control" type="text" name="load[1][grossweight]" value="<? =set_value('load[1][grossweight]')?>" placeholder="Gross Weight" >
<p><?=form_error('load[1][grossweight]'); ?></p>
</td>
</tr>
</tbody>
</table>
<input class="btn btn-success" onclick="addRow(this.form);" type="button" value="Add Item" /></strike>
<input id="getrates" type="submit" class="btn btn-primary btn-lg" name="getrate" value="Get Rate" />
</form>
Javascript Jquery
<script type="text/javascript">
var rowNum = 1;
function addRow(frm) {
rowNum ++;
var row = '<tr id="rowNum'+rowNum+'"><td>'+rowNum+'</td><td><input class="form-control" type="text" name="load['+rowNum+'][grossweight]" value="<?=set_value('load[rowNum][grossweight]')?>" placeholder="Gross Weight" ></td></tr>';
jQuery('#item-row').append(row);
}
function removeRow(rnum) {
jQuery('#rowNum'+rnum).remove();
}
</script>
Codeigniter Controller
// Posted array
$loads = $this->input->post('load');
if(!empty($loads))
{
foreach($loads as $id => $data)
{
$this->form_validation->set_rules('load['. $id .'][grossweight]', 'Gross Weight', 'required');
}
}
if ($this->form_validation->run() === FALSE)
{
$this->load->view('air/index', $data);
}
Related
In a view, When we route a button to a function inside the controller, how can we pass two or more values from present during that view.
I was practicing creating a result management system of students. In the view routed from index of ResultController, we have link options to view mark sheet of class ..or individual student. When we click on select class, it redirects to a view where there is two dropdowns to choose the class and batch of students. When we choose respected class and batch, the values class_id and batch_id is routed to function result inside ResultControler, we select students from that class and batch.. and respected subjects and return a view. In that view, we show the marksheet of students(if theres one), and below I have included a button to add marks/create marksheet.
But, I am so confused how I can pass those class_id and batch_id to create function inside ResultController, from the button.
public function index()
{
return view('resultmainpage');
}
public function choose()
{
$classes= Sclass::all();
$batches= Batch::all();
return view('chooseclassbatchresult',compact('classes','batches'));
}
public function result(Request $request)
{
$classid = $request->class;
$batchid = $request->batch;
//dd($batchid);
$students =Student::where('sclass_id',$classid)
->where('batch_id', $batchid)
->whereHas('subject')
->get();
$class= Sclass::findOrFail($classid);
return view('showstudentresult',compact('students','class','classid','batchid'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
// I need class_id and batch_id here
// dd($classidd);
$students = Student::where('sclass_id',$classid)
->where('batch_id',$batchid)
->whereDoesntHave('subject')
->get();
//dd($students);
Route:
Route::get('/rms','MainPageController#index')->name('rms');
Route::get('results/choose','ResultController#choose')->name('chooseresult');
Route::post('/showstudentresult','ResultController#result')->name('showstudentresult');
Route::resource('results','ResultController');
chooseclassbatchresult.blade.php
#extends('layout')
#section('content')
<h1>Please Choose the Class and Respected Batch Of Student For Result</h1>
</br>
</br>
<form action="{{route('showstudentresult')}}" method="post">
#csrf
<p>
<label>Class Name</label>
<select name='class'>
#foreach($classes as $class)
<option value="{{$class->id}}">{{$class->name}}</option>
#endforeach
</select>
</br>
</p>
<p>
<label>Batch</label>
<select name='batch'>
#foreach($batches as $batch)
<option value="{{$batch->id}}">{{$batch->batch}}</option>
#endforeach
</select>
</p>
</br>
<input type="submit" value="View">
</form>
</br>
</br>
</br>
<h1>OR</h1>
<h3>
<button><a href={{route('students.create')}}>Add New Student</a></button>
</h3>
#endsection
Showstudentresult.blade.php
#extends('layout')
#section('content')
<table border=1>
<thead>
<tr>
<th>S.N</th>
<th>Name</th>
<th>Roll NO</th>
#foreach($class->subjects as $subject)
<th>{{$subject->name}}</th>
#endforeach
<th>Total Mark</th>
<th>Percentage</th>
<th>Division</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php $id = 1; ?>
#foreach($students as $student)
<tr>
<td><?php echo $id;?></td>
<td>{{$student->name}}</td>
<td>{{$student->roll}}</td>
#foreach($student->subjects as $subject)
<th>{{$subject->pivot->mark}}</th>
#endforeach
<td>{{$student->result->total_mark}}</td>
<td>{{$student->result->percentage}}</td>
<td>{{$student->result->division}}</td>
<td>
<table>
<tr>
<td>
<button>Edit</button>
</td>
<td>
<form action="{{route('students.destroy',$student->id)}}" method="post">
#csrf
#method('DELETE')
<input type="submit" value="Delete"
onclick="return confirm('Are you sure you want to delete the student?')">
</form>
</td>
</tr>
</table>
</td>
</tr>
<?php $id++ ?>
#endforeach
</tbody>
</table>
</br>
</br>
<button><a href={{results.create}}>Create New</a></button>
#endsection
As Ross Wilson suggested in comment
I would suggest creating a separate page similar to
chooseclassbatchresult with a form that submits the data to create
Add a route in your routes files like:
Route::post('/createPage', 'ResultController#createPage')->name('createPage');
In ResultController add the following function:
public function createPage(Request $request)
{
// Get your required ids here
$classid = $request->class;
$batchid = $request->batch;
//dd($classid);
//dd($batchid );
}
In your chooseclassbatchresult view add another form like below
<form action="{{ route('createPage') }}" method="post">
#csrf
<p>
<label>Class Name</label>
<select name='class'>
#foreach($classes as $class)
<option value="{{$class->id}}">{{$class->name}}</option>
#endforeach
</select>
</br>
</p>
<p>
<label>Batch</label>
<select name='batch'>
#foreach($batches as $batch)
<option value="{{$batch->id}}">{{$batch->batch}}</option>
#endforeach
</select>
</p>
</br>
<input type="submit" value="View">
</form>
Thank you for your response. I got a way around my question. I learned that you can use input type="hidden" to carry those values back to the controller.
Create a route:
Route::post('/create_res', 'ResultController#create_res')->name('results.create_res');
In the View, chooseclassbatchresult.blade.php
<form action="{{route('results.create_res')}}" method="POST">
#csrf
<input type="hidden" name="classid" value="{{$classid}}">
<input type="hidden" name="batchid" value="{{$batchid}}">
<input type="submit" value="Create Mark Sheet">
</form>
In the Result Controller;
public function create_res(Request $request){
$classid = $request->classid;
$batchid = $request->batchid;
$students = Student::where('sclass_id',$classid)
->where('batch_id',$batchid)
->whereDoesntHave('subject')
->get();
$classes= Sclass::findOrFail($classid);
//dd($students);
return view('addmarksheet',compact('students','classes'));
}
Am creating a Billing System using Codeigniter. Here i want to select product using droupdown that product details show without refresh the page like ajax. My code is following:
View
<?php
foreach($productlists as $product)
{
?>
<tr>
<td>1</td>
<td>
<select class="form-control" name="product_id">
<option>--Select Payment--</option>
<option value="<?php echo $product['sno']; ?>"><?php echo $product['product_name']; ?></option>
</select>
</td>
<td><input type="number" name="product_order_qty" class="form-control" value="1"></td>
<td>0.8</td>
<td>0.8</td>
<td><input type="number" name="product_price" class="form-control" value="100"></td>
<td><input type="number" name="product_total" class="form-control" value="101.60"></td>
<td><a class="btn btn-primary waves-effect waves-light btn-xs"><i class="fa fa-plus"></i> Add</a></td>
</tr>
<?php } ?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
//Ajax code here
</script>
And Pass the product id to controller
controller
function productdetails()
{
$id = $_GET['product_id'];
$data['product'] = $this->orders->productdetails($id);
$data['title'] = "View Customer Order";
$this->load->view('customer_order_view',$data);
}
Please help me to pass the product_id using Ajax.
Thanks in advance
<select class="form-control" name="product_id" onchange="getProduct(this.value)">
<option>--Select Payment--</option>
<option value="<?php echo $product['sno']; ?>"><?php echo $product['product_name']; ?></option>
</select>
//your jquery function
function getProduct(product_id){
alert(product_id);
$.ajax({
url: "your_url",
type: "POST",
data:
{
id:product_id,
}
}).done(function( data )
{
// alert(data);
});
}
I want to know how to fetch name of checkboxes from dynamically created form. First I am displaying each records of data in a separate form then If records need to be deleted we will be able to delete. I am able to give different name as somename+id. But how do I know which name is going in the controller. As It is not clear that which name is going in the controller, I am not able to delete the record. I am doing it in laravel 5.4. Here is my code -
#if (isset($allcolors))
#foreach ($allcolors as $color)
<tr>
<form method="post" action="/delete">
{{csrf_field()}}
<input type="hidden" name="_method" value="DELETE">
<td>
<span class=""><input type="checkbox" name="deletecolor[{{$color->id}}]" value="{{$color->id}}"></span>
</td>
<td>
<div style="background:{{$color->web_color}}">a</div>
</td>
<td>{{$color->color_name}}</td>
<td>
<button type="submit" class="btn btn-danger">
Delete
</button>
</td>
</form>
</tr>
#endforeach
#endif
then on form submission I want to fetch which I am doing like this -
public function destroy(Request $request)
{
//
$id = $request->input('deletecolor');
$affected = DB::update("DELETE FROM vehicle_color where id = ?", [$id]);
//echo $affected==1?"Successfully Deleted":"Delete Fail";
}
I see you have $color->id. Why not delete them based on that?
#if (isset($allcolors))
#foreach ($allcolors as $color)
<tr>
<form method="post" action="/delete">
{{csrf_field()}}
<input type="hidden" name="_method" value="DELETE">
<td><span class=""><input type="checkbox" name="deletecolor[{{ $color->id }}]" value="{{$color->id}}"></span></td>
<td><div style="background:{{$color->web_color}}">a</div></td>
<td>{{$color->color_name}}</td>
<td><button type="submit" class="btn btn-danger" data-toggle="modal" data-target="#colorDelPopup">Delete</button></td>
</form>
</tr>
#endforeach
#endif
And in your controller:
public function destroy(Request $request)
{
$ids = $request->input('deletecolor');
// Instead of raw SQL, you can use the query builder to make your life a bit easier
$affected = DB::table('vehicle_color')->whereIn('id', $ids)->delete();
//echo $affected==1?"Successfully Deleted":"Delete Fail";
}
This should do the trick.
Hello I want to delete all the rows from the database which are being checked by the user but whenever I do this I get this error "Missing argument 1 for App\Http\Controllers\UploadController::deleteSelectedUploads()". I know why this error is coming because I haven't passed any id to it and if I pass any id to it then it says Undefined variable: upload. Please help me out. I know I have been posting a lot of questions nowadays and later solve it on my own but this is the only way of learning I guess.
Controller
public function deleteSelectedUploads($id) {
//$uploads = $this->upload->get($id);
// $checkboxes = $this->request->input('checkbox[]');
// if((Auth::user()->role=='admin')) {
// $this->upload->deleteUpload($id);
// } else {
// Session::flash('message', "Oooopsss ... ! seems like you don't have the authority to
// <span style=font-weight:bold;'><u>DELETE</u></span> this file.
// <span style='font-size:15pt; font-weight:bold;'> Contact ADMIN </span>");
// return redirect('home');
// }
$checkbox = Input::all();
foreach($checkbox['checkbox'] as $id) {
Upload::where('id', $id)->delete();
}
Session::flash('message', 'You File has been deleted');
return redirect('home');
}
View
<th><form action="/delete-selected-uploads" method="get">
<input type="checkbox" name="checkbox" onClick="toggle(this)"/>
<input type="submit" class="btn btn-block btn-success btn-xs" value="Delete all checked">
</form>
</center>
</th>
<td>
<input type="checkbox" name="checkbox[]" id="{{ $upload['id'] }}" value="{{$upload['id']}}">
</td>
Route
Route::get('/delete-selected-uploads', 'UploadController#deleteSelectedUploads');
Your problem is that you maybe put the parameter to
The function public function deleteSelectedUploads($id) { it need the parameter $id.
And you call this function avoid ::deleteSelectedUploads().
One solutions can be put not requerid the $id parameter as public function deleteSelectedUploads($id = '') {
UPDATE: Try in the view:
<th>
<form action="/delete-selected-uploads" method="get">
<input type="checkbox" name="checkbox" onClick="toggle(this)"/>
<input type="submit" class="btn btn-block btn-success btn-xs" value="Delete all checked">
<td>
<input type="checkbox" name="checkbox[]" id="{{ $upload['id'] }}" value="{{$upload['id']}}">
</td>
</center>
</th>
</form>
I hope help you.
On my form each row has it's on submit button and you need to check the check box before you can delete it else it should through error.
Question: My checkbox is in_array but if I do not check the box and then press submit it does not through the codeigniter form_validation error. I have used $this->form_validation->set_rules('selected[]', 'Selected', 'required'); But error not showing up.
What is the best solution in making it getting form_validation error to work?
View
<?php echo form_open('admin/design/layout/delete'); ?>
<?php echo validation_errors('<div class="alert alert-danger">', '</div>'); ?>
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<td style="width: 1px;" class="text-center">
<input type="checkbox" onclick="$('input[name*=\'selected\']').prop('checked', this.checked);" />
</td>
<td>Layout Name</td>
<td class="text-right">Action</td>
</tr>
</thead>
<tbody>
<?php if ($layouts) { ?>
<?php foreach ($layouts as $layout) { ?>
<tr>
<td class="text-center"><?php if (in_array($layout['layout_id'], $selected)) { ?>
<input type="checkbox" name="selected[]" value="<?php echo $layout['layout_id']; ?>" checked="checked" />
<?php } else { ?>
<input type="checkbox" name="selected[]" value="<?php echo $layout['layout_id']; ?>" />
<?php } ?>
</td>
<td><?php echo $layout['name']; ?></td>
<td class="text-right">
<button type="submit" class="btn btn-danger">Delete</button>
Edit
</td>
</tr>
<?php } ?>
<?php } else { ?>
<?php } ?>
</tbody>
</table>
</div>
</div>
<?php echo form_close(); ?>
Controller Function
public function delete() {
$this->load->library('form_validation');
$this->form_validation->set_rules('selected[]', 'Selected', 'required');
if ($this->form_validation->run() == FALSE) {
echo "Error";
$this->get_list();
} else {
$selected_post = $this->input->post('selected');
if (isset($selected_post)) {
foreach ($selected_post as $layout_id) {
}
echo "Deleted $layout_id";
$this->get_list();
}
}
}
It won't validate per field. selected[] selector in rules means, when you submit your form, it should be at least one selected item. Now you have submit buttons, which are independently submit the form, no matter where are they in the dom, and which checkboxes are selected.
Currently it the same, as you would have one submit button at the end.
I would add some javascript, and set if the checkbox is not selected, you can disable that field:
<script>
$(function() {
$('input:checkbox').on('change', function() {
if($(this).is(':checked')) {
$(this).closest('tr').find('button:submit').prop('disabled', false);
}
else {
$(this).closest('tr').find('button:submit').prop('disabled', true);
}
})
})
</script>
And add disabled to your submit buttons:
<button type="submit" class="btn btn-danger" disabled>Delete</button>
It's not a server side validation, but you can achieve to prevent push the button next unchecked checkboxes.