Nie*_*man 3 javascript php ajax jquery function
我有一个关于PHP函数,jQuery和AJAX的问题.如果我的php索引中有一个按钮,如下所示:
<input type="submit" value="Download" id="download"/>
Run Code Online (Sandbox Code Playgroud)
我有另一个PHP文件(dubs.php),其中包含:
<?php
function first(){
echo 'first';
}
function second(){
echo 'second';
}
?>
Run Code Online (Sandbox Code Playgroud)
我的jQuery,像这样:
$(document).ready(function(e) {
$("#download").click(function(){
$.ajax({
type: "GET",
url: "dubs.php",
});
});
});
Run Code Online (Sandbox Code Playgroud)
如何告诉我的AJAX请求选择第二个函数?
我不知道如何做到这一点,我已经尝试过"success: first()"或者用过"success: function(){ first() }"但是没有用.
Dee*_*epu 14
在你的ajax中传递一些参数来识别你想要使用的功能
$("#download").click(function(){
$.ajax({
type : "POST",//If you are using GET use $_GET to retrive the values in php
url : "dubs.php",
data : {'func':'first'},
success: function(res){
//Do something after successfully completing the request if required
},
error:function(){
//If some error occurs catch it here
}
});
});
Run Code Online (Sandbox Code Playgroud)
并在你的PHP文件中
您可以data通过ajax 检索发送中的值并执行以下操作
if(isset($_POST['func']) && $_POST['func']=='first'){
first();
}
else{
second();
}
Run Code Online (Sandbox Code Playgroud)