Is there a way to access a PHP class method using JavaScript via jQuery?

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'm trying to see if there is a way to access the class method bll/index.php

directly from within the script, rather than posting the parameters in order to access it.

+2


a source to share


3 answers


It looks like you are confused about web development.

JavaScript (if not specified) is a "client" language, which means that it is executed in the clients' browser AFTER the 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 ​​that only work on the server side. So to answer your question, no, you cannot directly force JavaScript to call a PHP function (server side), but you can post it to a PHP page, which in turn will execute whatever function you like.

Now if you say is it possible (although not recommended) then my answer is: -



Yes

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

In computer science, anything is possible.

Google for more details.

+2


a source


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

Depending on how much you use it, it might be a bit more and you still have to use .post()

either .get()

or .ajax()

.

From Wikipedia:



JSON-RPC is a JSON encoded remote procedure call protocol. It is a very simple protocol (and very similar to XML-RPC), defining only a few data types and commands. Unlike XML-RPC or SOAP, it allows bi-directional communication between service and client by treating increasingly similar peers and allowing peers to call each other or send notifications to each other. It also allows multiple calls to be sent to a peer that can be denied.

Example of message structure (also from Wikipedia):

--> { "method": "echo", "params": ["Hello JSON-RPC"], "id": 1}
<-- { "result": "Hello JSON-RPC", "error": null, "id": 1}

      

+1


a source


PHP runs on the server as part of the HTTP request. JavaScript (and your ajax) is run on the client after the page has loaded. If you want to generate new data in PHP after the page is loaded based on the form content on your page, you need to interact with PHP via ajax just like you do.

You can abstract from it, but in the end you will still use $.post()

or similar.

0


a source







All Articles