CodeIgniter drop down list - codeigniter

I have two drop down lists in my form and after input data to all fields and click submit button, second drop down gives an form error to input data to that field. I'm getting data to those drop downs from separate two tables in database. Table names are "borrowers" and "books" from database.
public function form_validation2()
{
//echo 'OK';
$this->load->library('form_validation');
$this->form_validation->set_rules("borrower_name", "Borrower Name", 'required');
$this->form_validation->set_rules("borrowed_book", "Borrowed Book", 'required');
$this->form_validation->set_rules("borrowed_date", "Borrowed Date", 'required');
$this->form_validation->set_rules("lending_date", "Lending Date", 'required');
if($this->form_validation->run())
{
//true
$this->load->model("main_model");
$data = array(
"borrower_name" =>$this->input->post("borrower_name"),
"borrowed_book" =>$this->input->post("borrowed_book"),
"borrowed_date" =>$this->input->post("borrowed_date"),
"lending_date" =>$this->input->post("lending_date")
);
if($this->input->post("insert"))
{
$this->main_model->insert_borrowed($data);
redirect(base_url() . "main/inserted2");
}
}
else
{
//false
$this->borrower_page();
}
}
public function borrower_page(){
// $this->load->view("borrower_page");
$this->load->helper('url');
if($this->input->post("btn_for_books_page")){
redirect(base_url().'main/index');
}
$this->load->model("main_model");
$data["fetch_data2"] = $this->main_model->fetch_data2();
$data['borrower_name'] = $this->main_model->get_detail_in_dropdown();
$data['book_name'] = $this->main_model->dropdown2();
$this->load->view("borrower_page", $data);
}
From view
<div class="form-group">
<label>Borrower Name</label>
<select class="form-control" name="borrower_name">
<?php
foreach($borrower_name as $row)
{
echo '<option value="'.$row->borrower_name.'">'.$row->borrower_name.'</option>';
}
?>
</select>
<!-- <input type="text" name="borrower_name" class="form-control"> -->
<span class="text-danger"><?php echo form_error("borrower_name"); ?></span>
</div>
<div class="form-group">
<label>Borrowed Book</label>
<select class="form-control" name="book_name">
<?php
foreach($book_name as $row1)
{
echo '<option value="'.$row1->book_name.'">'.$row1->book_name.'</option>';
}
?>
</select>
<!-- <input type="text" name="borrowed_book" class="form-control"> -->
<span class="text-danger"><?php echo form_error("borrowed_book"); ?></span>
</div>

You have to name your form inputs correct. Try changing:
<select class="form-control" name="book_name">
To:
<select class="form-control" name="borrowed_book">
Change it in your view since you are fetching "borrowed_book" in your controller.

Related

laravel routing, view not found

when am adding, it records saves to database, but when return to view is says ($store_id is undefined)
Blade:
<form action="{{ route("machine-slots.create", $machineSlotId) }}" method="POST">
#csrf
<input type="hidden" name="store_id" value="{{ $store_id }}">
<div class="modal-body ct-modal-form">
<div class="form-group">
<label for="slot_id" class=" form-control-label">Slot ID</label>
<input type="number" id="slot" name="slot_id"
placeholder="Enter Slot ID" class="form-control">
</div>
<div class="form-group ct-form-dropdown">
<label for="assign-item" class="form-control-label">Assign Item</label>
<select class="form-control" name="product_id">
<option value="" disabled selected>Select Items</option>
#forelse ($products as $i => $product)
<option value={{$product->id}}>{{$product->name}}</option>
#empty
#endforelse
</select>
</div>
<div class="form-group">
<label for="quantity" class=" form-control-label">Quantity</label>
<input type="number" id="quantity" name="current_count"
placeholder="Enter Quantity" class="form-control"
onkeyup="this.value = this.value.toUpperCase();">
</div>
<div class="form-group">
<label for="max-quantity" class=" form-control-label">Max Quantity</label>
<input type="number" id="max-quantity" name="max_count"
placeholder="Enter Max Quantity" class="form-control"
onkeyup="this.value = this.value.toUpperCase();">
</div>
</div>
<div class="modal-footer">
<input type="hidden" value="{{ $machineSlotId }}" name="machine_address_id">
<button type="submit" class="btn btn-primary">Add</button>
</div>
</form>
Controller:
public function store(Request $request, $machineSlotId)
{
//get all machineslot data
$machineSlots = $this->getMachineSlotData1($machineSlotId);
$products = $this->getProductsData();
$store_id = Machine::where([Machine::COLUMN_MACHINE_ADDRESS_ID => $machineSlotId])->first(['store_id'])->store_id;
if($request->input('current_count') > $request->input('max_count'))
{
//display text
Session::flash('warning', "Quantity cannot be higher than max Quantity. Please Try again");
//display return
return view('machine-slots', compact('machineSlotId', 'machineSlots', 'products', 'store_id'));
}
else
{
//create new record
MachineSlot::create($request->all());
//display text
Session::flash('success', "Table has been updated.");
//display return
//return view('machine-slots', compact('machineSlotId', 'machineSlots', 'products', 'store_id'));
return redirect()->route('machine-slots.index')->with(compact('machineSlotId', 'machineSlots', 'products', 'store_id'));
}
}
Web:
`
Route::post('/machine-slots/{id}', [App\Http\Controllers\UI\MachineSlotController::class, 'store'])->name('machine-slots.create');
Error
Heres the Update:
Blade
no Changes
Controller: Add on Index
public function index()
{
//check user Data
if(Auth::user() != null)
{
//get user ID
$userID = User::findOrFail(auth()->user()->id)->first(['id'])->id;
//check store is existing, based of passed ID
$isStoreExisting = Store::where('user_id', $userID)->first(['id'])->id;
//check machine is Existing, based on passed ID
$isMachineExisting = Machine::where('store_id', $isStoreExisting)->first(['machine_address_id'])->machine_address_id;
//get machine data
$machineSlot = MachineSlot::where('machine_address_id', $isMachineExisting)->first();
$machineSlots = MachineSlot::where('machine_address_id', $isMachineExisting)->paginate(5);
//fix paginate error
return view('machine-slots',compact('machineSlot', 'machineSlots'));
}
}
public function store(Request $request, $machineSlotId)
{
//get all machineslot data
$machineSlots = $this->getMachineSlotData1($machineSlotId);
$products = $this->getProductsData();
$store_id = Machine::where([Machine::COLUMN_MACHINE_ADDRESS_ID => $machineSlotId])->first(['store_id'])->store_id;
if($request->input('current_count') > $request->input('max_count'))
{
//display text
Session::flash('warning', "Quantity cannot be higher than max Quantity. Please Try again");
//display return
return view('machine-slots', compact('machineSlotId', 'machineSlots', 'products', 'store_id'));
}
else
{
//create new record
MachineSlot::create($request->all());
//display text
Session::flash('success', "Table has been updated.");
//display return
return view('machine-slots', compact('machineSlotId', 'machineSlots', 'products', 'store_id'));
}
}
Error: Data duplicates every time i refresh the page

multi Input search form

First, I try to check if the two fields 'id' and 'flowid' in form are in the same row or not. If they are in the same row, bring all the data in the row into the view. If not the same, then return and display a message that your inputs don't match. I'm having issues with this. Please advise.
Migration
Schema::create('letters', function (Blueprint $table) {
$table->id()->unique();
$table->string('flowid');
$table->string('id_number');
$table->string('letter_type');
$table->string('name_en');
$table->string('name_ar');
$table->string('nationality_en');
$table->string('nationality_ar');
$table->string('jobt_en');
$table->string('jobt_ar');
$table->string('to_en');
$table->string('to_ar');
$table->date('date_issue');
$table->date('hdate');
$table->date('salary_next');
$table->string('email');
$table->string('iban')->nullable();
$table->string('endser')->nullable();
$table->string('endser1')->nullable();
$table->string('salary_b_l')->nullable();
$table->string('housing_l')->nullable();
$table->string('trans_l')->nullable();
$table->string('mob_all_l')->nullable();
$table->string('other_l')->nullable();
$table->string('total_salary_l')->nullable();
$table->timestamps();
});
Model
class Letter extends Model
{
use HasFactory;
protected $guarded=[];
}
Controller
public function verify(Request $request)
{
$id = $request->id;
$flowid = $request->flowid;
if ($id != "") {
$letter = Letter::where('flowid', 'LIKE', '%' . $id . '%')->get();
if (!empty($flowid)) {
$letter->where('flowid', '=', $flowid);
}
$letter = Letter::findOrFail($letter());
return view('layouts.verify', compact('letter'));
}
}
View
<form action="{{ route('letter.verfiy') }}" method="GET">
{{-- #csrf --}}
<div class="form-group">
<label class="control-label col-sm-2" for="id">
Certificate Code:
</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="id"
placeholder="Type certificate Code">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="flowid">
Flow Employee ID:
</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="flowid"
placeholder="Type Flow ID">
</div>
</div>
<button type="submit" class="btn btn-primary">check</button>
</form>
Route
Route::any('/verfiy', [LetterController::class, 'verify',])->name('letter.verfiy');
Try that
$id = $request->id;
$flowid = $request->flowid;
if (!empty($id) {
$letter = Letter::find($id);
if (!empty($flowid)) {
if($letter->flowid === $flowid){
return view('layouts.verify', compact('letter'));
}
return 'Error'; // return error or redirect
}
}
check if the two fields 'id' and 'flowid' in form are in the same row or not
I believe a basic where clause should do the job for you here because you are checking id and flowid in one row.
$letter = Letter::where('id', $id)->where('flowid', $flowId)->first();
if (! $letter) {
return "Letter not found";
}
return $letter;

How to input multiple value with checkbox in CodeIgniter?

I have prepared the form to be inputted to the database, but specifically for multiple checkboxes. I've found a similar case with the solutionbut not with the algorithm that I use
Here it is my controller
class Ruang extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model("m_ruang");
$this->load->library('form_validation');
if($this->session->userdata('status') != "login"){
redirect(base_url("login"));
}
}
public function index()
{
$data["ruang"] = $this->m_ruang->getAll();
$this->load->view('admin/ruang/index.php', $data);
}
public function add()
{
$ruang = $this->m_ruang;
$validation = $this->form_validation;
$validation->set_rules($ruang->rules());
if ($validation->run()) {
$ruang->save();
$this->session->set_flashdata('success', 'Berhasil ditambahkan');
}
$this->load->view("admin/ruang/add_ruang");
}
Here it is my models
class M_ruang extends CI_Model
{
private $_table = "ruang";
public $id_ruang;
public $ruang;
public $kapasitas_kuliah;
public $kapasitas_ujian;
public $layout;
public $fasilitas;
public function getAll()
{
return $this->db->get($this->_table)->result();
}
public function getById($id)
{
return $this->db->get_where($this->_table, ["id_ruang" => $id])->row();
}
public function save()
{
$post = $this->input->post();
$this->id_ruang = uniqid();
$this->ruang = $post["ruang"];
$this->kapasitas_kuliah = $post["kapasitas_kuliah"];
$this->kapasitas_ujian = $post["kapasitas_ujian"];
$this->layout = $post["layout"];
$this->fasilitas = $post["fasilitas"];
$this->db->insert($this->_table, $this);
}
and here part of form view
<form action="<?php base_url('ruang/add') ?>" method="post" enctype="multipart/form-data" >
<div class="form-group">
<label for="ruang">Nama Ruang</label>
<input class="form-control <?php echo form_error('ruang') ? 'is-invalid':'' ?>"
type="text" name="ruang" placeholder="Masukkan nama ruangan" />
<div class="invalid-feedback">
<?php echo form_error('ruang') ?>
</div>
</div>
<div class="form-group">
<label for="kapasitas_kuliah">Kapasitas Kuliah</label>
<input class="form-control <?php echo form_error('kapasitas_kuliah') ? 'is-invalid':'' ?>"
type="number" name="kapasitas_kuliah" min="0" placeholder="Tentukan kapasitas kuliah" />
<div class="invalid-feedback">
<?php echo form_error('kapasitas_kuliah') ?>
</div>
</div>
<div class="form-group">
<label for="kapasitas_ujian">Kapasitas Kuliah</label>
<input class="form-control <?php echo form_error('kapasitas_ujian') ? 'is-invalid':'' ?>"
type="number" name="kapasitas_ujian" min="0" placeholder="Tentukan kapasitas ujian" />
<div class="invalid-feedback">
<?php echo form_error('kapasitas_ujian') ?>
</div>
</div>
<div class="form-group">
<label for="layout">Layout</label>
<input class="form-control"
data-inputmask="'mask': ['99 x 99']" data-mask
type="text" name="layout" placeholder="Tentukan layout ruangan" />
</div>
<div class="form-group">
<label for="fasilitas">Fasilitas Tersedia</label> <br>
<input type="checkbox" name="fasilitas[]" value="Proyektor"> Proyektor
<br>
<input type="checkbox" name="fasilitas[]" value="Papan Tulis"> Papan Tulis
<br>
<input type="checkbox" name="fasilitas[]" value="Jam Dinding"> Jam Dinding
<br>
<input type="checkbox" name="fasilitas[]" value="AC"> AC
<br>
<input type="checkbox" name="fasilitas[]" value="Kipas Angin"> Kipas Angin
<br>
<input type="checkbox" name="fasilitas[]" value="Tong Sampah"> Tong Sampah
<div class="invalid-feedback">
<?php echo form_error('fasilitas') ?>
</div>
</div>
<input class="btn btn-success" type="submit" name="btn" value="Save" />
</form>
This really hinders my project, I hope someone can help
You can use the following line too :
$fasilitas = implode(',', $this->input->post( 'fasilitas' , TRUE ) );
If you can save fasilitas in your database as string. Then you can implode fasilitas array with comma separated as shown below:
$this->fasilitas = implode(',',$post["fasilitas"]);
it will stored in back-end side(Database) something like that.
Proyektor,Papan Tulis
I hope this will works for you.
You Can Use This to Get fasilitas as array :
$fasilitas = $this->input->post('fasilitas'); // Like array('AC','Proyektor','Kipas Angin');
In order for you to get all the checked boxes store in database, write this code.
$values = $post['fasilitas'];
$fasilitas = "";
foreach($values as $val)
{
$fasilitas .= $val . ", ";
}
Then store $fasilitas to db.
$data = array(
'fasilitas' => $fasilitas,
);
$this->db->insert('table_name', $data);
Hope that helps :)

I want to display data in textfield. when search data have

I want to display data in textfield . Not all data. When search by name and it have data then went to display data in textfield. And search form and want to display form is same form.
<form action="postAuth" method="post" enctype="multipart/form-data">
<div class="input-group">
<input type="text" class="form-control" name="productname" placeholder="Search Product"> <span class="input-group-btn">
<button type="submit" class="btn btn-default" name="search">
<span class="glyphicon glyphicon-search"></span>
</button>
</span>
</div>
<div class="form-group">
<label for="ProductName" >Product Name :</label>
<input type="text" class="form-control" name="ProductName">
</div>
</form>
route
Route::post("postAuth", ['as' => 'search' , 'uses'=> 'ProductController#postAuth']);
That's my controller
public function postAuth(Request $request)
{
//check submit
$update = $request->get('update',false);
if($update){
return $this->update($request);
}
$productname = $request->input('productname');
$product = DB::table('products')
->where('product_name','LIKE','%'.$productname.'%')
->get();
if($product->count() > 0)
return redirect()->to('/update')->withDetails($product)->withQuery($productname);
else
$request->session()->flash('alert-danger','No Data Found!');
return redirect()->to('/update');
}
can anyone help me please
Here what you exactly want : Just for example
<div class="form-group">
{{Form::label('name', trans('admin.venue.fields.name'),['class' => 'col-md-4 control-label'])}}
<div class="col-md-6">
{{Form::text('name',old('name', isset($venue) ? $venue->name : ''),['class' => 'form-control'])}}
</div>
</div>
Model function example :
public function save(Request $request) {
try {
$this->validate($request, Venue::rules(), Venue::messages());
$venue = Venue::saveOrUpdate($request);
if($venue !== false) {
if($request->get('continue', false)) {
return redirect()->route('admin.venue.edit', ['id' => $venue->id])->with('success', trans('admin.venue.save_success'));
} else {
return redirect()->route('admin.venue.index')->with('success', trans('admin.venue.save_success'));
}
} else {
return back()->with('error', "Unable to save venue")->withInput();
}
} catch (\Exception $ex) {
return back()->with('error', "Unable to save venue")->withInput();
}
}
Hope it is useful.
use session() in the value field of ProductName input tag and with some logic, for example
view file:
#if(session()->has('data'))
#if(count(session('data')))
#foreach(session('data') as $data)
<input type="text" class="form-control" name="ProductName" value="{{ $data->prodcut_name }}">
#endforeach
#else
<input type="text" class="form-control" name="ProductName" value="No Data Available">
#endif
#else
<input type="text" class="form-control" name="ProductName">
#endif
and in Controller:
public function postAuth(Request $request) {
$productname = $request->input(productname);
$product_search = Products::where('product_name', $productname)->get();
if($product_search) {
return redirect()->back()->with('data', $product_search);
}
}

how to compare current and old password both are same or not before updation in DB using Codeigniter

I used this HTML for developing Form
<form method="post" action="<?php echo site_url('patients/change_patient_password'); ?>/<?php echo $this->session->userdata('patientDiseasesId'); ?>" class="form-horizontal">
<div class="form-body">
<div class="form-group">
<label class="col-md-3 control-label">Current Password</label>
<div class="col-md-4">
<input type="text" required class="form-control" name="password" placeholder="Current Password">
<?php $pass = form_error('password'); if(isset($pass)): ?><span class="help-block"><?php echo form_error('password'); ?></span><?php endif; ?>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">New Password </label>
<div class="col-md-4">
<input type="text" required class="form-control" name="password_confirm" placeholder="New Password">
<?php $pass_confirm = form_error('password_confirm'); if(isset($pass_confirm)): ?><span class="help-block"><?php echo form_error('password_confirm'); ?></span><?php endif; ?>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">ReType New Password</label>
<div class="col-md-4">
<input type="text" required class="form-control" name="password_confirm2" placeholder="ReType New Password">
<?php $pass_confirm2 = form_error('password_confirm2'); if(isset($pass_confirm2)): ?><span class="help-block"><?php echo form_error('password_confirm2'); ?></span><?php endif; ?>
</div>
</div>
</div>
<div class="form-actions">
<div class="row">
<div class="col-md-offset-3 col-md-9">
<button type="submit" class="btn green">Update Password</button>
<button type="button" class="btn default">Cancel</button>
</div>
</div>
</div>
</form>
also please have a look my code controller.
public function change_patient_password($patientId){
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('password', 'Password', 'required|is_unique[patients.password]');
$this->form_validation->set_rules('password_confirm', 'New Password', 'required|matches[password_confirm2]');
$this->form_validation->set_rules('password_confirm2', 'ReType New Password', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->change_patient_password_form();
}
else
{
$this->load->model('patients_model');
$data['password_updated'] = $this->patients_model->change_patient_password_model($patientId);
if($data['password_updated']==true){
$data['success']=true;
$data['main_content']= 'change_patient_password_form';
$this->load->view('includes/patient_template',$data);
}else{
$data['error']=true;
$data['main_content']= 'change_patient_password_form';
$this->load->view('includes/patient_template',$data);
}
}
here is the final model code
public function change_patient_password_model($patientId){
$data = array(
'password' => md5($this->input->post('password_confirm'))
);
$this->db->where('patientDiseasesId', $patientId);
return $this->db->update('patients', $data);
}
I see you're using "is_unique" in your form validation. Will each password be unique?
How I would do this, is to query the DB before we update it, and check if the passwords match. Like so.
public function change_patient_password_model($patientId)
{
// Get this users info
$query = $this->db->get_where('patients', array('patientDiseasesId' => $patientId));
if($query->num_rows() > 0)
{
// We have the patient, so check if the passwords match
$current_password = $query->row('password');
$inputted_password = md5($this->input->post('password_confirm'));
if( $current_password != $inputted_password)
{
// They are not the same, so update it...
$data['password'] = $inputted_password;
$this->db->where('patientDiseasesId', $patientId);
return $this->db->update('patients', $data);
}
else
{
// They are the same...
return false;
}
}
else
{
// No patients with this ID
return false;
}
}
I hope this helps.

Resources