PHP函数与jQuery AJAX? [英] PHP Function with jQuery AJAX?

查看:120
本文介绍了PHP函数与jQuery AJAX?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于一个问题,PHP函数,jQuery和AJAX。 如果我在我的PHP指数按钮是这样的:

I have a question regarding, PHP functions, jQuery and AJAX. If I have a button in my php index like this:

    <input type="submit" value="Download" id="download"/>

和我还有一个PHP文件(dubs.php),其中包含这样的:

And I have another php file (dubs.php) that contains this:

<?php
function first(){
    echo 'first';
}
function second(){
    echo 'second';  
}
?>

和我的jQuery的,像这样的:

And my jQuery, like this:

$(document).ready(function(e) {
    $("#download").click(function(){
        $.ajax({
            type: "GET",
            url: "dubs.php",
        });
    });
});

我如何告诉我的AJAX请求来选择,例如第二个功能?

How do I tell my AJAX request to select for example the second function?

我不知道如何做到这一点,我已经试过用的成功:第一个()的成功:功能(){第一个()},但没有奏效。

I have no idea on how to do this, I've tried it with "success: first()" or with "success: function(){ first() }" but that did not work.

推荐答案

在你的AJAX传递一些PARAMS确定哪些功能要使用这样

In your ajax pass some params to identify which function you want to use like this

    $("#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 if you want to do something after successfully completing the request
            },
            error:function(){
              //If some error occurs catch it here
            }
        });
    });

而在你的PHP文件

And in your php file

您可以retrive在数据值通过发送Ajax和执行以下操作

you can retrive the values in data send via ajax and do the following

if(isset($_POST['func']) && $_POST['func']=='first'){
    first();
}
else{
    second();
}

这篇关于PHP函数与jQuery AJAX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆