3

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.

5 Answers 5

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 completing 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();
}
Sign up to request clarification or add additional context in comments.

7 Comments

You'll also need a success function to work with the returned data
Thanks it works, but still one small question: Any idea why it turns into the else statement instead of the if? It runs second(); instead of first(); but I don't see why it does that.
Can you try var_dump($_POST['func']) to see any spaces are associated with that?.If so use trim() in php to remove spaces.
It returns NULL, I get the following error: <br /> <b>Notice</b>: Undefined index: func in <b>C:\Users\user\Desktop\USBWebserver v8.5\8.5\root\UploadifyZWP\dubs.php</b> on line <b>6</b><br /> NULL
Give the same key of data in ajax while you use the var_dump().Meaning if data: {'func':'first'} then var_dump($_POST['func']) same key of data in $_POST.I think you have changed the key in the ajax
|
1

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.

Comments

1

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!

});

Comments

1

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();
}

?>

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.