2

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.

6
  • 3
    No, JS is client site, PHP is server site. See: How does PHP work Commented May 17, 2010 at 12:33
  • 1
    I know that, I was just thinking if we can post, may be we can call a function also Commented May 17, 2010 at 12:36
  • You want to pass a string to the server and eval it? Sounds dangerous. Commented May 17, 2010 at 12:43
  • @Felix Kling dude its possible by using xmlrpc. Commented May 17, 2010 at 13:07
  • @piemesons: Not few people ask whether they can call PHP from JS and don't know the difference between client and server site. XML-RPC, JSON-RPC or any other form of RPC are just a more structured forms of transmitting data. You still have to post parameters in a way. You cannot directly call a PHP function from JS. Commented May 17, 2010 at 13:16

3 Answers 3

2

It sounds like you're getting confused with developing on the web.

JavaScript (unless specified) is a "client-side" language, meaning that it runs on the clients browser AFTER has page has been compiled and sent from the server. This means that there is no PHP/ASP in the code, only HTML and JavaScript.

PHP, ASP, JSP are server-side languages which only run on the server-side. So to answer your question, no, you can't directly get the JavaScript to call a PHP (server-side) function, but you can post to a PHP page which will then in turn carry out whatever function you like.

Now if you are saying is it possible (Although it's not recommendable ) then my answer is:--

YES

Calling PHP methods from JavaScript is possible by using XML-RPC. You can call PHP methods remotely with JavaScript by setting up an XML-RPC server (a PHP page) and then access that server within JavaScript. There are PHP XML-RPC libraries and JavaScript XML-RPC .js files.

In computer science everything is possible.

Google it for more details.

Sign up to request clarification or add additional context in comments.

2 Comments

in computer science everything is possible. No it is not. Ever heard the halting problem ? ;)
@Felix Kling for that computer science has a term exception.. :) :-P
1

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}

1 Comment

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.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.