更改下拉列表显示调用 php 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6108495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:22:51  来源:igfitidea点击:

on Change of dropdown list showld call the php function

phpjquery

提问by Indu

On change of the dropdown list, A php function should called.

更改下拉列表时,应调用 php 函数。

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.

在 PHP 函数中,我会做一些计算。稍后我需要设置文本框的值,否则 PHP 函数应该返回一个值,该值应该在 javascript 中捕获并将该值设置为文本框控件。

My html function is

我的 html 函数是

<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

并且在PHP函数中放置在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.

所以现在请[提供给我 JQuery 函数来调用这个 Php 函数,以便在下拉列表中的每次更改时调用 GetTotalPL() 函数。

回答by Imran Khan

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...

您不能从 js 或 html 调用 php 函数,但是您可以通过对 php 函数的 ajax 调用来执行此操作,您可以在其中执行计算,然后将结果值返回给 js,这样您就可以通过 js 执行到 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...
}

回答by Hamid

Insert your function in a php file to excute , then you could call php file with ajax ( i suggest u use jQuery )

将您的函数插入到一个 php 文件中执行,然后您可以使用 ajax 调用 php 文件(我建议您使用 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>