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>
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?
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
need your help for this ... My homepage have 3 divs, #Header, #Content, #Footer. All the other pages are being opened inside the #Content div. In one of those pages I have a form with two select lists and one submit button. Just want to click the button and then return another page into the #Content div, showing the values that I select before. Like this:
The origin is: 1
The destiny is: 1
But this code returns the following ...
Notice: Undefined variable: origin in ...
Notice: Undefined variable: destiny in ...
Note: This is working if I don't open the page inside the #Content div
my Html:
<form id="myform" name="myform" action="values.php" method="POST">
<select id="origin" name="origin">
<option value="0" selected>-- Select Origin --</option>
<option value="1">Portugal</option></select>
<select id="destiny" name="destiny">
<option value="0" selected>-- Select Destiny --</option>
<option value="1">Lisboa</option></select>
<input id="btSubmit" name="btSubmit" type="submit" value="search!">
</form>
my Function:
$(document).ready(function(){
$('#btSubmit').click(function(e) {
e.preventDefault();
var url = $('#myform').attr('action');
var method = $('#myform').attr('method');
$.ajax({
type: method,
url: url,
data: $('#myform').serialize(),
success: $('#content').load(url)
});
});
});
my values.php page:
<?php
if(isset($_POST['origin']) || isset($_POST['destiny']))
{
$origin = $_POST['origin'];
$destiny = $_POST['destiny'];
}
echo 'The origin is:' . $origin . '<br>';
echo 'The destiny is:' . $destiny;
?>
You should not call load again - you have already called it essentially with $.ajax and received the results. So you need just display them in the content:
success: function (data) {
$('#content').html(data);
}
You should use success callback function correctly. Accept response in callback method and set it in your div
success: function (data) {
$('#content').html(data);
}
Additionally, You should perform your operation with form submit event.
$('form#myform').on('submit', function (e) {
instead of
$('#btSubmit').click(function(e) {
As Andrei mentioned you have to use
success: function (data) {
$('#content').html(data);
}
because calling success: $('#content').load(url) triggers a new GET request. When GET request reaches php code $_POST is not set and your variables are not initialized so you get the message from php:
Notice: Undefined variable: origin in
This is what I've got so far:
This function that returns data from the model:
function get_user2()
{
$this->load->model('my_model');
$id=$this->input->post('users1');
$users2=$this->my_model->get_relations($id);
return $users2;
}
the model function:
function get_relations($usr)
{
$this->db->where('id',$usr);
$rel=$this->db->get('relacion');
if($rel->num_rows!=0)
{
$relacion=array();
foreach ($rel->result_array() as $row)
{
$relacion[]=array(
'id'=>$row['id'],
'username1'=>$row['username1'],
'username2'=>$row['username2'],
);
}
return $relacion;
}else{
return false;
}
}
and in my view:
<select name="users1" id="drop1">
<?php
if($opciones!=false){
foreach ($opciones as $row) {
echo '<option value="'.$row['user_id'].'">'.$row['username'].'</option>';
}
}
?>
</select>
<script src="jquery.js"></script>
<script type="text/javascript">
$("#drop1").change(function(){
$.ajax({
type: "POST",
url: "example.com/CI/index.php/evaluation/get_user2",
data: "users1="+$('#drop1').val(),
success: function(){
alert('it works!');
}
});
});
</script>
I want to fill a second dropdown with the options returned by the controller function, but the ajax request doesn't do anything so I haven't even got to that part. Can someone help me spot what's wrong? I already tested the controller and model's function and they work. And could you tell me how to fill the second dropdown's options?
Thank you very much!
Well I've a very similar code in one project to get some cities depending on the island the user choose. So wen the select changes, load the cities and enables the second select. The main difference is the way you pass the data.
<script type="text/javascript">
$("#idisla").change(function(){
if($("#idisla").val()!=""){
var dato=$("#idisla").val();
$.ajax({
type:"POST",
dataType:"html",
url:base_url+"admin/centros/municipios_select",
data:"idislajs="+dato,
success:function(msg){
$("#idmunicipio").empty().removeAttr("disabled").append(msg);
callback();
}
});
}else{
$("#idmunicipio").empty().attr("disabled","disabled");
}
});
</script>
The function only looks cities from the selected islad (id)
function municipios_select()
{
//el idIsla viene dado por el value del combo islas
$isla = $this->input->post('idislajs');
$data['municipios'] = $this->municipios_model->obtenMunicipios($isla);
echo $this->load->view("site/municipios_select",$data, TRUE);
}
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());
});