views:

75

answers:

3

I have a JavaScript script:

    $("#feedbacksubmit").click(function() {
    if($("#frmfeedback").valid()) {
        var tname = $("#name").val();
        var temail = $("#email").val();
        var tphone = $("#phone").val();
        var tcontent = $("#content").val();
        var tsend = $(this).attr('ts');
        $.post ( "bll/index.php",
                {  action: 'mailfeedback', name: tname, email: temail, phone: tphone, content: tcontent, send: tsend },
                function(data) {                    
                    $('.msgbox').html(data);
                    $("#frmfeedback")[0].reset();
            });         
        return false;       
    }
});

However, I am trying to see if there is a way to access the class method of bll/index.php directly from the script, instead of posting parameters, to access it.

A: 

PHP runs on the server as part of an HTTP request. JavaScript (and your ajax) runs on the client after the page has loaded. If you want to generate new data in PHP after page load based on the contents of a form on your page, you have to interface with the PHP through ajax just like you're doing.

You could abstract it out, but you'll still be using $.post() or similar in the end.

Adam Backstrom
+1  A: 

You could use JSON-RPC to send calls to your server in a more structured way. But that also means that you have to use an RPC server on the server side (or at least have some functionality that parses the RPCs, there should be some available for PHP).

Depending on how much you use this, this could be a bit over kill and you would still have to use .post() or .get() or .ajax().

From Wikipedia:

JSON-RPC is a remote procedure call protocol encoded in JSON. It is a very simple protocol (and very similar to XML-RPC), defining only a handful of data types and commands. In contrast to XML-RPC or SOAP, it allows for bidirectional communication between the service and the client, treating each more like peers and allowing peers to call one another or send notifications to one another. It also allows multiple calls to be sent to a peer which may be answered out of order.

Example of the structure of the messages (also from Wikipedia):

--> { "method": "echo", "params": ["Hello JSON-RPC"], "id": 1}
<-- { "result": "Hello JSON-RPC", "error": null, "id": 1}
Felix Kling
It'll be interesting if there are projects in PHP land similar to Java RMI to have a direct object to object communication across the wire. That would mean a [de]serialization layer sits on both the client and server sides, with stubs of server side objects available in client side Javascript.
Anurag
+2  A: 
piemesons
*in computer science everything is possible*. No it is not. Ever heard the halting problem ? ;)
Felix Kling
@Felix Kling for that computer science has a term exception.. :) :-P
piemesons