tags:

views:

756

answers:

6

This may be a simple answer, but I'm using jquery's $.ajax to call a PHP script. What i want to do is basically put that PHP script inside a function and call the PHP function from javascript.

<?php 
if(isset($_POST['something'] {
    //do something
}
?>

to this

<?php
function test() {
    if(isset($_POST['something'] {
         //do something. 
    }
}
?>

How would i call that function in javascript? Right now i'm just using $.ajax with the PHP file listed.

A: 

You are going to have to expose and endpoint (URL) in your system which will accept the POST request from the ajax call in jQuery.

Then, when processing that url from PHP, you would call your function and return the result in the appropriate format (JSON most likely, or XML if you prefer).

casperOne
A: 

I would stick with normal approach to call the file directly, but if you really want to call a function, have a look at JSON-RPC (JSON Remote Procedure Call).

You basically send a JSON string in a specific format to the server, e.g.

{ "method": "echo", "params": ["Hello JSON-RPC"], "id": 1}

which includes the function to call and the parameters of that function.

Of course the server has to know how to handle such requests.
Here is jQuery plugin for JSON-RPC and e.g. the Zend JSON Server as server implementation in PHP.


This might be overkill for a small project or less functions. Easiest way would be karim's answer. On the other hand, JSON-RPC is a standard.

Felix Kling
A: 
$.post(
    "http://example.com/folder/your_file.php",     // url
    { something : "foobar", foo : "bar" },         // post-data
    function(data){                                // response
        console.log(data);
    }
);

See more examples at http://api.jquery.com/jQuery.post/

qualbeen
Imo this is what the OP is already doing. But now he wants to call a certain function.
Felix Kling
+5  A: 

Use $.ajax to call a server context (or URL, or whatever) to invoke a particular 'action'. What you want is something like:

$.ajax({ url: '/my/site',
         data: {action: 'test'},
         type: 'post',
         success: function(output) {
                      alert(output);
                  }
});

On the server side, the action POST parameter should be read and the corresponding value should point to the method to invoke, e.g.:

if(isset($_POST['action']) && !empty($_POST['action'])) {
    $action = $_POST['action'];
    switch($action) {
        case 'test' : test();break;
        case 'blah' : blah();break;
        // ...etc...
    }
}

I believe that's a simple incarnation of the Command pattern.

karim79
Gotcha. So you can't directly choose with js which function to call in PHP, you can only use PHP to grab the post value and call a function that way. Thanks
Catfish
@Karim sorry for this comment but you can solve it http://stackoverflow.com/questions/2584029/jquery-ajax-success-function-returns-null and http://stackoverflow.com/questions/2583369/is-this-a-valid-url-parameter-in-jquery-ajax
Pandiya Chendur
A: 

I am at the beginning of building a little framework that maps provided php functions to javascript and takes care of all the communication, but I've not finished it yet.

See here: http://sourceforge.net/projects/mydlo/

[this is edited, I commited a prealpha version just now :)]

naugtur
A: 

You can't call a PHP function with Javascript, in the same way you can't call arbitrary PHP functions when you load a page (just think of the security implications).

If you need to wrap your code in a function for whatever reason, why don't you either put a function call under the function definition, eg:

function test() {
    // function code
}

test();

Or, use a PHP include:

include 'functions.php'; // functions.php has the test function
test();
DisgruntledGoat