我有一个关于 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 文件中
您可以检索通过ajax 发送的data
中的值并执行以下操作
if(isset($_POST['func']) && $_POST['func']=='first'){
first();
}
else{
second();
}
关于javascript - 带有 jQuery AJAX 的 PHP 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16894192/