populate second dropdown from first dropdown value and print value of input in codeigniter - codeigniter-2

this is my contoller
public function add()
{
$data['gis'] = $this->Mymodel->get_gis_start();
$data['side'] = 'tampil/side';
$data['content']='ds/add_ds';
$this->load->view('tampil/main',$data);
}
this is my view
<td><select class="form-control select2" style="width: 100%;" name="pick_from" id="pick" onchange="populate(this.id,'drop')"><?php foreach($gis as $cit): ?>
<option value="<?php echo $cit->start_point; ?>"><?php echo $cit->start_point; ?></option><?php endforeach; ?>
</select></td>
<td><select class="form-control select2" style="width: 100%;" name="pick_till" id="drop"></select></td>
<td><input name="total_loads" class="form-control select2" style="width: 100%;" id="tl1" value="" onkeyup="sum(); sum();" placeholder="Total Loads Picked"></td>
this is my model
function get_gis_start()
{
$query = $this->db->get('gis');
return $query->result();
}
I want to populate second select with values selected from first dropdown and want to put the value of third input from database
my data base is
This is the image of the database from which i want to fetch start_point and fill the select with id="pick" and end_point and fill the values related to start_point in select id="drop" and put the value of total_distance to the input value="values related to first select and second select" how can I do that kindly tell my I am very much struck

See the answer with the link given here Codeigniter dropdown

Related

Pass a date input value from View to Controller using Codeigniter

I am trying to give the user to select the date he wants, and it will display the reservations for the day he selected. I am using Codeigniter, the MVC framework.
How am I setting a default value of the current date for an input date selection?
How am I sending the value to the controller and then I am using it for a function in the model.
<form id="form_reDate" name="form_reDate" method="POST" action="form_reDate.php">
<?php $r_date=#date('d-m-y'); ?>
<label for="reservations">Display reservations for :
<input type="date" id="date" name="re_date" style="margin-left: 0;"
min="2020-01-01" max="2020-12-31" value="<?php $r_date?>">
</label>
</form>
Controller:
public function form_reDate(){
$newDate = date("Y-m-d",strtotime($this->input->post('re_date')));
$data['tables'] = $this->Hosting_model->get_tables($newDate);
}
If you're using the default html of input type="date" to set its value you only use this php code.
<?php echo date('Y-m-d') ?>
to sum it:
<input type="date" id="date" name="re_date" style="margin-left: 0;"
min="2020-01-01" max="2020-12-31" value="<?php echo date('Y-m-d') ?>">
and for question #2 on how to send your input value to your Controller. Just make sure you get the data before sending it to your Model.

Get DropDownList value into POST method

I am working on this ASP.NET Core MVC where I have this DropDownLisit which gets its values from Controller using ViewBag.DishTypes. However, upon submitting the form, the POST method is not getting the value of the option selected in the DropDownList. The code snippets are as follows:
Controller: GET Method
var allDishTypes = _context.DishType
.ToList()
.Select(dt => new SelectListItem { Value = dt.DishTypeId.ToString(), Text = dt.DishTypeName.ToString() }).ToList();
ViewBag.DishTypes = allDishTypes;
return View();
View
<form asp-controller="Home" asp-action="AddMenuItems">
<div class="row">
<label class="my-1 mr-2" for="inlineFormCustomSelectPref">Dish Type</label>
<div class="input-group">
<div class="fg-line form-chose">
<label asp-for="DishTypeId" class="fg-labels" for="DishTypeId">Dish Type</label>
<select asp-for="DishTypeId" asp-items="ViewBag.DishTypes" class="form-control chosen" data-placeholder="Choose Dish Type" required name="dishtype" id="dishtype">
<option value=""></option>
</select>
</div>
</div>
....
Controller: POST Method
[HttpPost]
public IActionResult AddMenuItems([Bind("DishTypeId, DishName, Cost")] Dishes dishesObj)
{
....
}
the POST method is not getting the value of the option selected in the DropDownList
Note that you specified a name=dishtype within your code. By this way, the field name is
always the same as this name attribute, i.e, dishtype instead of DishTypeId, which will not be recognized by ASP.NET Core by default.
To fix that issue, simply remove that attribute such that it uses asp-for to generate the name attribute automatically:
<select asp-for="DishTypeId" asp-items="ViewBag.DishTypes"
class="form-control chosen" data-placeholder="Choose Dish Type" required
name="dishtype" id="dishtype"
>
<option value=""></option>
</select>

Populate input field from a dropdown using codeigniter

chapter_number | chapter_title |
1 | Introduction |
2 | Number System |
<select type="text" name="chapter_number" class="form-control form-control-line" value="">
<option selected="option" disabled="selected">Select</option>
<option value="1">1</option>
<option value="2">2</option>
</select
<input type="text" name="chapter_title" id="title" class="form-control"/>
above is my table and next is my code the select option and the input field. I want that when I select '1' automatically the input field will print chapter title 'Introduction' and If I select '2' the input field will became 'Number System'.
If you want to get data from database you need to call ajax on onchange event of select box.
Add "setinput" class to select box
<select type="text" name="chapter_number" class="form-control form-control-line setinput" value="">
Add below script
$('body').on('change','.setinput',function(){
$.ajax({
url: 'your_base_url/controller_name/your_method',
type:'POST',
data:{id:$(this).val()},
success:function(result){
$('input[name="chapter_title"]').val(result);
}
})
});
Controller method would be like below
public function your_method(){
$id = $this->input->post('id');
$this->db->select('*');
$this->db->from('database_table_name');
$this->db->where('chapter_number',$id);
$result = $this->db->get()->row();
echo $result->chapter_title;
}
I hope, it would be helpful to you.

Problem with form option selected where parameters in the url

Hi I have problem with my multiple select search form.
The parameters are in the url
urlhomepage.../search?cars=1&cars=2&cars=4
I do not know how to get parameters url in form option as selected
<select multiple="multiple" name="cars[]" id="select2" placeholder="">
#foreach($names as $name)
<option value="{{$name->id}}">{{$name->title}}</option>
#endforeach
</select>
I want to get the effect so that the form options are marked on the basis of a url query
First you should pass variable as array as cars[] like below
urlhomepage.../search?cars[]=1&cars[]=2&cars[]=4
Inside php you can get cars array form query string
<?php
$cars = request('cars',[]);
?>
And put cars value as auto selected in select
<select value="{{$cars}}" multiple="multiple" name="cars[]" id="select2" placeholder="">

Using Webmatrix, how do I save data not displayed in the select in a foreach loop?

I am desperate! I have a list of people with names and a unique ID. I need to display just the first and last name in a drop down list and then when the user selects the name they want from the list and submits, I would like to create a new record in a different table and add:
first name
last name
unique ID
I just don't know the syntax to do it. I have tried everything and this is the closest I get which works for one record in the list but multiple records in the list fails.
<select class="form-control" name="">
<div>#foreach(var Team in MyTeam){
<option value="">#Team.FirstName #Team.LastName </option>
<input type="hidden" name="FirstName" value=#Team.FirstName>
<input type="hidden" name="LastName" value=#Team.LastName>
<input type="hidden" name="TeamID" value=#Team.UserID>
<input type="hidden" name="ReferrerName" value=#Team.UserID>
}
</div>
You do not need the hidden fields, they will not work.
<select class="form-control" name="formUser">
#foreach(var Team in MyTeam){
<option value="">#Team.FirstName #Team.LastName </option>
}
</select>
Then, you need to add post code
if(IsPost){
var userID = Request["formUser"];
var sqlselect = "SELECT * FROM YourTable Where UserID = #0";
var sqldata = db.QuerySingle(sqlselect, userID);
var firstname = sqldata.FirstName;
var lastname = sqldata.LastName;
//And so on to get your variables then you can use them to insert
}

Resources