Is there a way to access a PHP class method using JavaScript through 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 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.
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.
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}
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.
精彩评论