My $this->input->post is not working when I changed the value of a dropdown menu (it dynamically display the record from db) it does not display any data and no errors too, but it's working fine if I assign a specific value to the where syntax.
Model:
public function getData() {
$where = $this->input->post('selected_holcode');
$query = $this->db->get_where("hrms.holiday", "holcode = '$where'")->result();
return $query;
Controller:
public function getHolidaylist() {
$data['record'] = $this->holiday_settings_model->getData();
$this->load->view('maintainance/holiday_settings/holiday_list', $data);
}
ajax (to display the record upon changing the dropdown menu)
<script type="text/javascript">
var controller = 'holiday_settings';
var base_url = '<?php echo site_url(); ?>';
function load_data_ajax(type){
$.ajax({
'url' : base_url + '/' + controller + '/getHolidaylist',
'type' : 'POST',
'data' : {'type' : type},
'success' : function(data){
var container = $('#holiday');
if(data){
container.html(data);
}
}
});
}
</script>
View:
<?php echo form_open('holiday_settings/getHolidaylist') ?>
<div id="holiday"></div>
<?php echo validation_errors(); ?>
<label><strong>Year</strong></label> <label><strong>:</strong></label>
<select id="syear" onchange="load_data_ajax(1)">
<?php
for ($x=2015; $x>=2008; $x--) {?>
<option><?php echo $x;?></option>
<?php } ?>
</select></br></br>
<select name="selected_holcode" onchange="load_data_ajax(1)">
<option value="SPL">SPL</option>
<option value="LGL">LGL</option>
<option value="CMP">CMP</option>
</select></br></br>
enter code here
If you say that static value makes it work then your controller and model functions are working correctly I guess, Maybe you should try this:
onchange="load_data_ajax(1)" //instead of writing this
You should just pass the value like this:
onchange="load_data_ajax(this.value)"
Or as I see you are using JQuery, you can also do this:
<select name="selected_holcode" class="toggler" > <!-- Remove the onchange event and add a new class to the element -->
<script type="text/javascript">
$(function(){ //on page load
$(".toggler").on("change", function(){
var value_to_send = $(this).val(); //get the selectbox value here
//your ajax call goes here
});
});
</script>
im not sure but try this:
<select name="selected_holcode" onchange="load_data_ajax(this.value)">
instead of : load_data_ajax(1)
or
onchange="load_data_ajax(this.options[this.selectedIndex].value)
if your Dropdown was filled with dynamic data from DB
// EDIT
And what do you want to do if syear is changed?
since you call the same function? and you should set it like this:
for ($x=2015; $x>=2008; $x--) {
echo "<option value='$x'>$x</option>";
}
Now you can get your value to your onchange function like i already said
Related
I am new to Ajax and JavaScript
I have a similar problem with my selects that depends on the other select but don't know where it is going wrong?
This is my Controller code- that passes the member_id to the model and it is supposed to return data back to the view selected.
public function getStates() {
$this->load->model('ReferenceModel');
$this->load->model('DailyModel');
$this->load->model('MembersModel');
$this->load->model('FundsModel');
$postData = array('member_id' => $this->request->getPost('member_id'));
$dataa = $this->FundsModel->getStates($postData);
echo json_encode($dataa);
}```
This is my AJAX Request Code
<script type='text/javascript'>
// baseURL variable
var baseURL= "<?php echo base_url();?>";
$(document).ready(function(){
// City change
$('#member_id').change(function(){
var member_id = $(this).val();
// AJAX request
$.ajax({
url:'<?=base_url()?>Historic/getStates',
method: 'post',
data: {member_id: member_id},
dataType: 'json',
success: function(response){
// Remove options
$('#id').find('Select Member').not(':first').remove();
// Add options
$.each(response,function(index,data){
$('#id').append('<option value="'+dataa['id']+'">'+dataa['fund']+'</option>');
});
}
});
});
});
</script>
Model
public function getStates($postData){
$sql = 'SELECT * FROM vw_funds_summary WHERE member_id =' .$postData['member_id'] ;
$query = $this->db->query($sql);
return $query;
}```
And My HTML Codes
<select id="member_id" name="country" class="form-control">
<option>Select Member</option>
<?php
for ($i=0;$i<$fMembers->num_rows();$i++){
echo "<option value='".$fMembers->row($i)->member_id."'>".$fMembers->row($i)->member_name."</option>";}
?>
</select>
<select class="form-control" id="id">
<option>Select Fund</option>
</select>
What is really wrong with my Code?
I have a view of both the funds and the members, that are giving the results as shown in the attached picture.
Or is there another way to do it without having to use AJAX?
I want to populate textbox values automatically from DB based on dropdown selection.....I have tried this, but after selecting dropdown the page is refreshing but the values are not populated onto textbox....need Help!!
Table
Controller:
public function isrexpense()
{
$data['query'] = $this->Isr_model->getstates();
$data['names'] = $this->Isr_model->getAllNames();
$data['query1'] = $this->Isr_model->test();
$this->load->view("header");
$this->load->view('Isr/isrexpense', $data);
$this->load->view("footer");
}
Model:
function test()
{
$this->db->select('da_hq');
$this->db->from('isr_policy_master');
$this->db->where('state', $this->input->post('state'));
$query = $this->db->get();
return $query->result();
}
View:
<select class="js-example-basic-single form-control">
<?php
foreach($names as $row)
{
echo '<option></option>';
echo '<option value="'.$row->name.'">'.$row->name.'</option>';
}
?>
</select>
<select class="state form-control" id="state" name="state">
<?php
foreach($query as $row)
{
echo '<option value="'.$row->state_code.'">'.$row->state_name.'</option>';
} ?>
</select>
<script>
$('#state').on('change', function(){
var mainselection = this.value; // get the selection value
$.ajax({
type: "POST", // method of sending data
url: "<?php echo site_url(); ?>/Isr/isrexpense",
data:'selection='+mainselection,
success: function(result)
{
$("#hqda").html(result);
}
});
});
</script>
<input id="hqda" name="hqda" class="form-control" required type="number">
Controller :
public function isrexpense()
{
$data['query'] = $this->Isr_model->getstates();
$data['names'] = $this->Isr_model->getAllNames();
$this->load->view("header");
$this->load->view('Isr/isrexpense', $data);
$this->load->view("footer");
}
public function getValFromDb()
{
$state = $this->input->post('selection');
$query1 = $this->Isr_model->test($state);
echo json_encode(array('data'=>$query1));
}
Model:
function test($state)
{
$this->db->select('da_hq');
$this->db->from('isr_policy_master');
$this->db->where('state', $state);
$query = $this->db->get();
$result = $query->result();
if(count($result)>0)
{
return $result[0]['da_hq'];
}
else
{
return '';
}
}
View :
<script>
$('#state').on('change', function(){
var mainselection = this.value; // get the selection value
$.ajax({
type: "POST", // method of sending data
url: "<?php echo site_url(); ?>/Isr/getValFromDb",
data:'selection='+mainselection,
success: function(result)
{
console.log(result); // check this in your console and if require than use JSON.parse to get value
$("#hqda").val(result.data);
}
});
});
</script>
Textboxes don't have an "innerHTML" property. If you want to set the content of a textbox, you should set its value property. In jQuery you do this using the .val() method.
Replace
$("#hqda").html(result);
with
$("#hqda").val(result);
Documentation: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input
and http://api.jquery.com/val/
P.S. You may also want to take a look at what your AJAX call returns - it's not clear whether it would return the content you're looking for or not - it appears to return a whole HTML view, rather than just a single value to place into a textbox.
How I would access this url index.php/backend/categories/get_categories/$id within a <select> dropdown onChange ?
I mean, if I will go to index.php/backend/categories/get_categories/1, it will output a table with all categories under component id 1.
What I want, is once i select an option from a dropdown list, i want it to submit and redirect to the same url, but with a specific ID from <option val="$id"> ?
To generate a dropdown list with CodeIgniter FORM helper:
<?php
echo form_open('backend/categories/get_categories');
$selectComponentData = array(0 => '-- '.lang('SELECT_COMPONENTS').' --');
$selectComponentJs = 'onChange="this.form.submit()" id="selectCatsByComponent"';
foreach($selectComponents as $option){
$selectComponentData[$option->id] = $option->name;
}
echo form_dropdown('selectCatsByComponent', $selectComponentData, 0, $selectComponentJs);
echo form_close();
?>
And output HTML is:
<form accept-charset="utf-8" method="post" action="http://127.0.0.1/backend/web/index.php/backend/categories/get_categories">
<select id="selectCatsByComponent" onchange="this.form.submit()" name="selectCatsByComponent">
<option selected="selected" value="0">-- Choose a component --</option>
<option value="1">E-commerce</option>
<option value="2">Content</option>
</select>
</form>
How to make it possible ?
if I dont get you wrong, You are asking something like this;
In controller;
public function get_categories()
{
if(isset($_POST) && !empty($_POST())
{
$id = $this->input->post('selectCatsByComponent');
// bla bla bla
$data['id'] = $id;
$this->load->view('whatever/whatever',$data);
}
else
{
//Your normal page before posting
}
}
Now you can access the id value in view like this
if(isset($id)) // if there is a $id loaded
{
// Your view accoring to $id
}
Note: I did not have an test environment while I was writing this code. So The code is not complate and there might be some mistakes
You can use ajax with jquery onchange function, see the below example:
$("#bussiness").live('change',function(){
var bus_id=$(this).val();
$.ajax({
url : "<?php echo base_url(); ?>setting/set_session/"+bus_id,
success:function(res) {
document.getElementById('output').HTML = res;
}
});
});
Ok I have a onchange event on a select field. It now works great. When the dropdown "networks" is changed it refreshes the second dropdown. I also want the ajax code at the top to trigger on page load as well as onchange so the second list gets populated. This is due to it being on an edit page. Here is the ajax call im using first
function get_cities(networks) {
$.ajax({
type: "POST",
url: "select.php", /* The country id will be sent to this file */
beforeSend: function () {
$("#folder").html("<option>Loading ...</option>");
},
//data: "idnetworks="+networks,
data: "idnetworks="+networks +"&doc="+ <?php echo $row_rs_doc['parentid']; ?>,
success: function(msg){
$("#folder").html(msg);
}
});
}
now the two dropdown boxes
<label for="networks"></label>
<select name="networks" id="networks" onChange='get_cities($(this).val())'>
<?php
do {
?>
<option value="<?php echo $row_rs_net['idnetworks']?>"<?php if (!(strcmp($row_rs_net['idnetworks'], $row_rs_doc['network']))) {echo "selected=\"selected\"";} ?>><?php echo $row_rs_net['netname']?></option>
<?php
} while ($row_rs_net = mysql_fetch_assoc($rs_net));
$rows = mysql_num_rows($rs_net);
if($rows > 0) {
mysql_data_seek($rs_net, 0);
$row_rs_net = mysql_fetch_assoc($rs_net);
};
?>
</select>
<select name="folder" id="folder">
</select>
You can use .trigger() to trigger a change event onto the select-box so the onchange code will be called like it would if the user just switched the option.
jQuery('#networks').trigger('change');
Just include this into the load event/function for the page.
jQuery(document).ready(function() {
jQuery('#networks').trigger('change');
});
I'm not 100% clear what you want but the standard way to do something with JQuery when the page is loaded, is to use
$(document).ready(handler)
This waits till the page is "ready" which is better.
So, in your document head you'd have something like this...
<script type="text/javascript">
$(document).ready( function(){
do_some_stuff();
});
</script>
Can't you just call get_cities($('#networks').val()) when the DOM is ready?
$(function() { // will be run by jQuery when DOM is ready
get_cities($('#networks').val());
});
On change of the dropdown list, A php function should called.
Inside the PHP function I will do some calculation. Later I need to set the value of a text box or else that PHP function should return a value that value shoul be catch in the javascript and set that valu to the textbox control.
My html function is
<select name="sltLeaveType" id="sltLeaveType" class="formSelect" onchange="TotalCountsOfPL(this.value)">
<?php
<option></option>
<option></option>
</select>
And in PHP function is placed in D:/Project/EmployeeDetails/EmpLeave.php
class clsGetTotalPL
{
function GetTotalPL($EmployeeId)
{
$query = "select count(leave_request_id) from hs_hr_leave_requests where leave_type_id='LTY002' AND employee_id=".$EmployeeId.";";
return $query;
}
So now please [prvide me the JQuery function to call this Php function
to get call the GetTotalPL() function on every change on the the dropdownlist.
You can't call a php function from js or html, but you can do that by ajax call to a php function where you can perform your calculation and then return the result value to js, so then you can do by js to html...
Update:
<select name="employee" id="employee" onchange="getIPL(this.value);">
<option value="">Select Employee</option>
</select>
function getIPL(id)
{
$.ajax({
type: "GET",
url: "EmpLeave.php",
data: "emp_Id =" + id,
success: function(result){
$("#somewhere").html(result);
}
});
};
// Empleave.php file....
if(isset($_GET['emp_Id'])){
GetTotalPL($_GET['emp_Id']);
}
function GetTotalPL($id){
// do your calculation...
}
Insert your function in a php file to excute , then you could call php file with ajax ( i suggest u use jQuery )
///////////////////////// foo.php
<?php
Function foo (){
// Your process
}
foo();
?>
///////////////////// text.html
<select name="test" id="test" >
<option>.....</option>
.
.
.
.
</select>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>
<script type="text/javascript">
$('#test').change(function(){
$.ajax({
type: "GET",
url: "foo.php",
success:function(){
// Your function after execute foo.php
}
})
})
</script>