Populate Form Based on dropdown value in Codeigniter - codeigniter

I have about 30 different forms that will be displayed in a wizard, a form will be displayed based on the value from the dropdown selected, if all forms are created in a view file it doesn't seem effective, I try to save each form in the database and it will Called based on the dropdown value but not as expected, please help with this problem.
this is the code example that I saved into the database:
<label>Yang Bertandatangan Dibawah ini:</label>
<div class="form-group row">
<label class="col-form-label col-lg-2">Nama</label>
<div class="col-lg-10">
<select name="pemberi" class="form-control select-search" required data-fouc>
<option value="">--pilih--</option>
<?php
if(is_array($pegawai)){
foreach($pegawai as $row){
?>
<option value="<?php echo $row->nip;?>"><?php echo $row->nama;?></option>
<?php
}
}
?>
</select>
</div>
</div>
<label>MEMERINTAHKAN:</label>
<div class="form-group row">
<label class="col-form-label col-lg-2">Kepada:</label>
</div>
<div class="form-group row">
<label class="col-form-label col-lg-2">Nama:</label>
<div class="col-lg-10">
<select name="penerima" class="form-control select-search" required data-fouc>
<option value="">--pilih--</option>
<?php
if(is_array($pegawai)){
foreach($pegawai as $row){
?>
<option value="<?=$row->nip;?>"><?=$row->nama;?></option>
<?php
}
}
?>
</select>
</div>
</div>
<div class="form-group row">
<label class="col-form-label col-lg-2">Jabatan:</label>
<div class="col-lg-10">
<select name="jabatan_penerima" class="form-control select-search" required data-fouc>
<option value="">--pilih--</option>
<?php
if(is_array($pembuat)){
foreach($pembuat as $row){
?>
<option value="<?=$row->id;?>"><?=$row->nama_jabatan;?></option>
<?php
}
}
?>
</select>
</div>
</div>
<label>Untuk:</label>
<div class="row">
<div class="col-md-9">
<div class="form-group">
<textarea rows="10" cols="4" class="form-control" placeholder="Isi Perintah" name="isisurat" id="isisurat" required></textarea>
</div>
</div>
</div>
The code runs well if it is directly written in source code, but different if called from the database
as expected if I write the code in page directly:
If I save the code in database and call based on value of dropdown,it will show error:

Why don't you use normal load views based on a condition like in your index you would do this:
VIEW ONE
VIEW TWO
// and so on ...
Then in your controller:
public function your_method($your_view)
{
$data['view'] = $this->load->view($your_view, null, TRUE);
// Setting third param as true will return the view as a string
$this->load->view('main_view', $data);
}
Now in your main view you have a variable called view of your requested view just echo it in your main content area.

Related

Laravel Livewire : livewire multiple root elements detected

I'm currently developing a website using laravel livewire
The website has a multi-site form page, and there's a dynamic dropdown on the inside of that multi-site form page
The problem is when the form is filled, it won't store the data into the database
When i do inspect the website there's an error that read :livewire multiple root elements detected
How do i fix this??
Livewire model :
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use App\Models\Kredit;
use App\Models\Biaya;
use App\Models\Produk;
use App\Models\Promo;
use App\Models\Motorcycle;
use App\Models\MotorcycleBrand;
use App\Models\Domisili;
class KreditMulti extends Component
{
public $brand_id;
public function render()
{
$domisilis = Domisili::all();
// $motorcycles = Motorcycle::all();
// $motorcycle_brands = MotorcycleBrand::all();
//for the dynamic dropdown
if($this->brand_id){
$motorcycle_brands = MotorcycleBrand::where('motorcycle_id', $this->brand_id)->get();
} else {
$motorcycle_brands = [];
}
return view('livewire.kredit-multi',
['domisilis'=>$domisilis])
->withMotorcycles(Motorcycle::all())
->with('motorcycle_brands', $motorcycle_brands);
}
}
The livewire blade php :
<div class="form-group row">
<label for="motorcycle" class="col-md-4">Merek motor</label>
<div class="col-md-6">
<select wire:model="brand_id" class="form-control">
<option value="" selected>Choose Motor</option>
#foreach ($motorcycles as $m)
<option value="{{$m->id}}">{{$m->motorcycle_name}}</option>
#endforeach
</select>
</div>
</div>
<br>
#if (count($motorcycle_brands) > 0)
<div class="form-group row">
<label for="motorcycle_brand" class="col-md-4 col-form-label text-md-right">Jenis Motor</label>
<div class="col-md-6">
<select class="form-control" name="motorcycle_brand_id">
<option value="" selected>Choose the motor version</option>
#foreach ($motorcycle_brands as $motor)
<option value="{{$motor->id}}" wire:key="motorcycle_brand{{$motor->id}}">{{$motor->motorcycle_brand_name}}</option>
#endforeach
</select>
</div>
</div>
#endif
<br>
all livewire components must be start with a <div> and end with a </div>.
actually you must wrap the whole <div> and </div> s into a single <div></div>
Your blade code has several roots
1.- <div class="form-group row"....>
2.- <br>
3.- #if (count($motorcycle_brands) > 0)...#endif
4.- <br>
Enclose all that stuff in a single root
<div>
<div class="form-group row"....>
<br>
#if (count($motorcycle_brands) > 0)...#endif
<br>
</div>
The error is as exact as it can be. A Livewire component simply can only have a single root element. Instead, you're adding 2, including 2 breaks.
Wrap the whole component into a single <div></div>, and then it should be fixed.
public $motorcycle_brands,$domisilis,$brand_id,$motorcycles;
public function render()
{
$this->domisilis = Domisili::all();
$this->motorcycles = Motorcycle::all();
if($this->brand_id){
$this->motorcycle_brands = MotorcycleBrand::where('motorcycle_id', $this->brand_id)->get();
} else {
$this->motorcycle_brands = [];
}
return view('livewire.kredit-multi');
}
Blade
#if (count($motorcycle_brands) > 0)
<div class="form-group row">
<label for="motorcycle_brand" class="col-md-4 col-form-label text-md-right">Jenis Motor</label>
<div class="col-md-6">
<select class="form-control" wire:model="brand_id">
<option value="" selected>Choose the motor version</option>
#foreach ($motorcycle_brands as $motor)
<option value="{{$motor->id}}">{{$motor->motorcycle_brand_name}}</option>
#endforeach
</select>
</div>
</div>
#endif

fetch datas from database based on selected data dropdown using laravel 8

hai everyone i want to fetch data (affected device name, device intended use, class of device) from database based on my selected dropdown (mda registration num),i get the registration numb er from database but i don't how to fetch other related datas based on registration num.How i can gets datas.Here my controller n create.blade.php,pls help me
create.blade.php
<div class="form-group row">
<label for="inputMedName col-auto" class="col-lg-3 col-form-label " for="mdaRegisNo1">1. MDA Device Registration No. <span style="color:red;">*</span></label>
<div class="col-6 col-lg-6">
<select name="mdaRegisNo" class="form-control " id = "mdaRegisNo1" select data-size="5" data-live-search="true">
<option > Select MDA Registration Number</option>
#foreach($devices as $key=>$device)
<option value="{{$key}}">{{$device->device_reg_no}}</option>
#endforeach
</select>
</div>
<input type='button' value='Seleted option' id='but_read'>
</div>
<div class="form-group row">
<label class="col-lg-3 col-form-label ">2. Affected Device Name <span style="color:red;">*</span></label>
<div class="col-9 col-lg-9">
<input name="devName" type="text" class="form-control" >
</div>
</div>
<div class="form-group row">
<label class="col-lg-3 col-form-label ">3. Device intended use <span style="color:red;">*</span></label>
<div class="col-9 col-lg-9">
<input name="devUse" type="text" class="form-control" >
</div>
</div>
<div class="form-group row">
<label class="col-lg-3 col-form-label ">4. Class of Device<span style="color:red;">*</span></label>
<div class="col-9 col-lg-9">
<input name="devClass" type="text" class="form-control" >
</div>
</div>
RecallControlle.php
public function create()
{
$recall = Recall::latest()->first();
$ref_recall = 'MDA/Recall/P'.$this->getRefSuffix($recall);
//$devices = DB::table('devices');
$devices = Device::get();
$post = Device::query();
$device = DB::table('devices')->select('device_reg_no')->distinct()->get()->pluck('device_reg_no')->sort();
//dd($devices);
//return json_encode($devices);
return view('recall.create',['devices'=>$devices])->with([
'ref_recall' => $ref_recall,
'ref_mpr' => ''
]);
}
You have to use js to request data when your dropdown been selected.
Follow this doc to request data: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
pass your selected value to your api
get result from api
append value to input

I want to get data to the dropdown by using another dropdown in the same page

I have a dropdown on my page to load districts. And I have another dropdown to load Divisions according to that selected district. The below code I have got all the divisions. But I want only the divisions according to the selected districts. How can I do?
#php
$districtAll=\App\District::all();
#endphp
<div class="row">
<label class="col-md-4 control-label" for="district">9. District</label>
<div class="col-md-8">
<select name="district">
<option type="text" class="detail-wp" value="">Select District</option>
#foreach($districtAll as $all)
<option value="{{$all->DISTRICT_ID}}">{{$all->DISTRICT}} </option>
#endforeach
</select>
</div>
</div>
<br>
#php
$getDsAll=\App\DsDivision::all();
#endphp
<div class="row">
<label class="col-md-4 control-label" for="ds_division">10. DS Division</label>
<div class="col-md-8">
<select name="ds_division" class="form-control">
<option type="text" class="detail-wp" value=""></option>
#foreach($getDsAll as $all)
<option value="{{$all->DIVISION_ID}}">{{$all->DIVISION}} </option>
#endforeach
</select>
</div>
</div>
You can do it using ajax request:
$('select[name=district]').on('change', function(){
$districtId = $(this).val();
$.get('/division/district/'+$districtId).done(function(res){
$.each(res, function (key, value) {
$('select[name=ds_division]').append(`<option value='` + value.id + `'>` + value.name +`</option>`);
});
});
to make this working you will need a route like
Route::get("/division/district/{district}", "DivisionController#getByDistrictId");
DivisionController#getByDistrictIdwill be
public function getByDistrictId($district)
{
return Division::where('district_id',$district)->get();
}
this code just to give you an idea of how you can do it.

Gentelella Combo Box

I am using combo box - select custom, using gentelella template. On example , gentelella select custom, we can type text on the combo box and the list on the combo box will change accordingly to the text we type.
But In my case, I can not type the text on the combo box. It won't work.
this is my view
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">NIK</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<select class="select2_single form-control" tabindex="-1">
<option></option>
<?php foreach ($listkary as $key => $value) { ?>
<option value="<? echo $value['idnik']?>"><? echo $value['kodenik']?> - <? echo $value['namakary']?></option>
<? } ?>
</select>
</div>
</div>

codeigniter select list generated from database error when submitting the form

I have created a form which has 2 select box which also generate the options from database but the problem is when I do click on submit button the select box becomes empty.
here is the codes:
View:
<ol class="breadcrumb-icon">
<li><i class="fa fa-home"></i> Home</li>
<li class="active"><i class="icon fa fa-inbox"></i> Add Courses</li>
</ol>
<div class="page-header">
<h1>Add Courses:</h1>
</div>
<div class="well">
<?php if(isset($error)) echo $error;?>
<?php
$this->load->helper('form');
$attributes = array('role' => 'form','class'=>'form-horizontal');
echo form_open_multipart('admin/addcourses',$attributes);
?>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Course Category:</label>
<div class="col-sm-10">
<select name="category" class="form-control">
<?php
foreach ($cat as $row)
{
echo "<option value='".$row->category_id."'>".$row->category_name."</option>";
}
?>
</select>
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Course Location:</label>
<div class="col-sm-10">
<select name="location" class="form-control">
<?php
foreach ($loc as $row)
{
echo "<option value='".$row->location_id."'>".$row->location_name."</option>";
}
?>
</select>
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Course Language:</label>
<div class="col-sm-10">
<select name="language" class="form-control">
<option value="English">English</option>
<option value="Arabic">Arabic</option>
</select>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Course Name</label>
<div class="col-sm-10">
<input type="text" name="coursename" class="form-control" id="inputPassword3" placeholder="">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Course Date:</label>
<div class="col-sm-10">
<input type="text" name="date" id="range-picker" readonly class="form-control kd-daterange">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Course Image</label>
<div class="col-sm-10">
<input type="file" name="course_image" class="upload" readonly class="form-control kd-upload">
<p>keep it empty to use the default image.</p>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Add Course</button>
</div>
</div>
</form>
</div>
</div> <!-- /.container laaast -->
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
<script src="js/extended.min.js"></script>
<script>
extended = new Extended();
</script>
Controller addcourses.php
public function addcourses()
{
if ($this->session->userdata('is_logged_in'))
{
$this->load->model('admin/read_cat_loc');
$data['cat']= $this->read_cat_loc->read_categories();
$data['loc']= $this->read_cat_loc->read_locations();
$this->load->view('admin/header_view');
$this->load->view('admin/addcourses_view',$data);
$this->load->view('admin/footer_view');
}
else {
redirect('admin/home');
}
}
model codes
class Read_cat_loc extends CI_Model {
public function read_categories() {
$query= $this->db->get('course_category');
return $query->result();
}
public function read_locations() {
$query= $this->db->get('course_location');
return $query->result();
}
}
ok solved:
I did this in main controller section :
$this->load->model('admin/read_cat_loc');
$data['cat']= $this->read_cat_loc->read_categories();
$data['loc']= $this->read_cat_loc->read_locations();
$this->load->view('admin/header_view');
$this->load->view('admin/addcourses_view',$data);
$this->load->view('admin/footer_view');
but in other areas where I have to reload the view I did this only:
$this->load->view('admin/header_view');
$this->load->view('admin/addcourses_view',$data);
$this->load->view('admin/footer_view');
without the model part to retain the data again ...
If you know better way It will be great. Thanks

Resources