我有一个关于 PHP 函数、jQuery 和 AJAX 的问题。
如果我的 php 索引中有一个像这样的按钮:
<input type="submit" value="Download" id="download"/>
我还有另一个 php 文件 (dubs.php),其中包含以下内容:
<?php
function first(){
echo 'first';
}
function second(){
echo 'second';
}
?>
我的 jQuery 是这样的:
$(document).ready(function(e) {
$("#download").click(function(){
$.ajax({
type: "GET",
url: "dubs.php",
});
});
});
我如何告诉我的 AJAX 请求选择第二个函数?
我不知道如何做到这一点,我已经尝试过"success: first()"
或与"success: function(){ first() }"
但这没有用。
在你的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
}
});
});
在你的 php 文件中
您可以检索其中的值data
通过ajax发送并执行以下操作
if(isset($_POST['func']) && $_POST['func']=='first'){
first();
}
else{
second();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)