php 如何从ajax调用php函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39341901/
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
How to call a php function from ajax?
提问by Josip Gòdly Zirdum
I am familiar of how to get ajax to go to a php page an execute a series of things and then return json data. However is it possible to call a specific function which resides in a given page?
我熟悉如何让ajax转到一个php页面并执行一系列事情然后返回json数据。但是,是否可以调用位于给定页面中的特定函数?
Basically what I want is to reduce the number of files in a project. So I can put a lot of common functions in one page and then just call whatever the function that I want at the moment.
基本上我想要的是减少项目中的文件数量。所以我可以把很多常用的函数放在一个页面中,然后调用我现在想要的任何函数。
回答by Nitheesh K P
For ajax request
对于ajax请求
1.Include Jquery Library in your web page. for eg:
1.在您的网页中包含 Jquery 库。例如:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
2.Call a function on button click
2.点击按钮调用函数
<button type="button" onclick="create()">Click Me</button>
3.while click on button ,call create function in javascript.
3.当点击按钮时,调用javascript中的create函数。
<script>
function create () {
$.ajax({
url:"test.php", //the page containing php script
type: "post", //request type,
dataType: 'json',
data: {registration: "success", name: "xyz", email: "[email protected]"}
success:function(result){
console.log(result.abc);
}
});
}
<script>
On the server side test.php file, the action POST parameter should be read and the corresponding value and do the action in php and return in json format e.g.
在服务端test.php文件中,读取action POST参数和对应的值,在php中执行action并以json格式返回eg
$regstration = $_POST['registration'];
$name= $_POST['name'];
$email= $_POST['email'];
if ($registration == "success"){
// some action goes here under php
echo json_encode(array("abc"=>'successfuly registered'));
}
回答by Vishnu Bhadoriya
You can't call a PHP function directly from an AJAX call, but you can do this:
您不能直接从 AJAX 调用中调用 PHP 函数,但您可以这样做:
<? php
function test($data){
return $data+1;
}
if (isset($_POST['callFunc1'])) {
echo test($_POST['callFunc1']);
}
?>
<script>
$.ajax({
url: 'myFunctions.php',
type: 'post',
data: { "callFunc1": "1"},
success: function(response) { console.log(response); }
});
</script>
回答by Rajendra Rajput
$(document).ready(function(){
$('#tfa_1117700').change(function(){
var inputValue = $(this).val();
var v_token = "{{csrf_token()}}";
$.post("{{url('/each-child')}}", { dropdownValue: inputValue,_token:v_token }, function(data){
console.log(data);
$('#each-child-html').html(data);
});
});
});
public function EachChild(Request $request){
$html ="";
for ($i=1; $i <= $request->dropdownValue; $i++) {
$html = $i;
}
echo $html;
}
回答by iliaz
If I understand correctly, yes you can. Put all your functions in one php file and have the ajax pass as a parameter which one you want to call. Then with a switch or if structure, execute the one you want.
如果我理解正确,是的,你可以。将所有函数放在一个 php 文件中,并将 ajax 作为参数传递给您要调用的函数。然后使用 switch 或 if 结构,执行你想要的。