最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - PHP Function with jQuery AJAX? - Stack Overflow

programmeradmin2浏览0评论

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"/>

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

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

And my jQuery, like this:

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

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.

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"/>

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

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

And my jQuery, like this:

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

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.

Share Improve this question edited Jan 6, 2014 at 11:16 웃웃웃웃웃 12k15 gold badges62 silver badges94 bronze badges asked Jun 3, 2013 at 10:00 Niek JonkmanNiek Jonkman 4974 gold badges11 silver badges23 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 14

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 after successfully pleting the request if required
            },
            error:function(){
              //If some error occurs catch it here
            }
        });
    });

And in your php file

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

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

This is what I would do:

Your PHP:

<?php

function first(){
    echo 'first';
}

function second(){
    echo 'second';  
}



  if (isset($_POST["first"])) first();
  if (isset($_POST["second"])) second(); //add 'else' if needed

?>

your jQuery:

$.post("dubs.php", {"first":true}, function(result) {
  $("#someDivToShowText").text(result);
});

Then, according to the object you send to $.post, the php file will know which function to run.

Try this in your PHP page:

<?php

function first(){
    echo 'first';
}

function second(){
    echo 'second';  
}
switch($_POST['func']) {
    case "first":
    first();
    break;
    case "second":
    second();
    break;
    default:
    // Define your default here }
?>

and this in your JS:

$(document).ready(function(e) {

    $("#download").click(function(){
        $.ajax({
            type: "GET",
            url: "dubs.php",
            data: {'func':'first'}
        });
    });

The func variable will tell php which function to run!

});

why don't you try to pass with data:{func:f1} and get it on php side and if f1 is there then fire the first function. Although you can send multiple:

jQuery:

$("#download").click(function(e){
    e.preventDefault(); // <----stops the page refresh
    $.ajax({
        type: "GET",
        url: "dubs.php",
        data:{'func':'f1'}
    });
});

PHP:

<?php

  function first(){
     echo 'first';
  }

  function second(){
     echo 'second';  
  }


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

?>

JS

$("#download").click(function(){
    $.ajax({
        type: "GET",
        url: "dubs.php",
        data: {'function':'first'}
    });
});


PHP

call_user_func($_GET['function']);


NOTE
Be careful with $_GET parameters, better check first the contents of $_GET

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>