javascript - 带有 jQ​​uery AJAX 的 PHP 函数?

标签 javascript php jquery ajax function

我有一个关于 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 - 带有 jQ​​uery AJAX 的 PHP 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16894192/

相关文章:

javascript - 谷歌地图标签和监听器事件驱动的 CSS

javascript - iframe 在另一个 iframe 中调用 javascript 函数?

javascript - 避免 jquery select 的类型错误

javascript - 如何在javascript for循环中打印大括号

php - 如何用增量值更新一组记录

java - 用PHP代替JAVA进行PKCS5加密

javascript - angularjs 方法 `$http.post` 是如何工作的?

php - 如何让ajax javascript在按下回车键时工作

jquery - 可能/如何使用 jquery.tabs() 将水平选项卡嵌套在垂直选项卡下?

javascript - 在下拉列表中显示的时间