[英]how to call one function in another php file by using ajax
<script>
$(document).ready(function(){
$("#amount").on('click', function(){
var amount = this.value;
$.ajax({
url: "ipg-util.php/createHash",
type: 'post',
data: { "amount": amount
},
success: function(response) { console.log(response); }
});
});
});
</script>
You can simply pass the Method to Call
as part of the data
property like so:您可以简单地将
Method to Call
作为data
属性的一部分传递,如下所示:
<script>
$(document).ready(function(){
$("#amount").on('click', function(){
var amount = this.value;
$.ajax({
url: "ipg-util.php",
type: 'post',
data: { "amount": amount, "callable": "createHash"},
success: function(response) { console.log(response); }
});
});
});
</script>
Then inside ipg-util.php
You can do something like this:然后在
ipg-util.php
里面你可以做这样的事情:
<?php
$callable = isset($_POST['callable']) ? $_POST['callable'] : "default";
$amount = isset($_POST['amount']) ? $_POST['amount'] : null;
switch($callable){
case "createHash":
$response = createHash(); // CALL createHash METHOD DEFINED HEREIN
break;
case "doAnotherThing":
$response = doAnotherThing(); // CALL doAnotherThing METHOD DEFINED HEREIN
break;
default:
$response = default(); // CALL default METHOD DEFINED HEREIN
break;
}
die(json_encode($response));
function createHash(){
}
function doAnotherThing(){
}
function default(){
}
$.ajax to call a server context or URL, whatever to invoke a particular 'action'. $.ajax调用服务器上下文或 URL,无论调用特定的“操作”。 What you want is something like and the below code as follows:
你想要的是像下面的代码如下:
$.ajax({ url: '/demo/websitepage',
data: {action: 'calltofunction'},
type: 'POST',
success: function(output) {
alert(output);
}
});
On the server side, the action POST parameter should be read and the corresponding value should point to the method to invoke and the below code is used for doing that:在服务器端,应该读取动作 POST 参数,并且相应的值应该指向要调用的方法,以下代码用于执行此操作:
if(isset($_POST['action']) && !empty($_POST['action'])) {
$action = $_POST['action'];
switch($action) {
case 'calltofunction' : test();break;
case 'blah' : blah();break;
// ...etc...
}
}
Hope so this will be a better reference for this: https://en.wikipedia.org/wiki/Command_pattern希望这将是一个更好的参考: https : //en.wikipedia.org/wiki/Command_pattern
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.