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.
Best Answer
Use
$.ajax
to call a server context (or URL, or whatever) to invoke a particular 'action'. What you want is something like:On the server side, the
action
POST parameter should be read and the corresponding value should point to the method to invoke, e.g.:I believe that's a simple incarnation of the Command pattern.