Okay some web searches show how to call a php script and pass parameters from within a javascript block of code, but I can’t find how to call a specific function in the php script
For example.
<?php
Function one() {
}
Function two() {
}
Function three() {
}
?>
From within js I want to execute Function two passing a value from a textbox, any ideas?
Thanks in advance.
The way I would do it is pass another param specifying which you want. eg action=two
Then when action == two {
two(other_args);
Thanks for the reply, was hoping to avoid using that approach,
Thus far have
if($.trim(username) != "") {
$.post('functions.php', {name: username}, function(data) {
alert(data);
});
}
Am using jquery, which works fine on calling functions.php and returning a result, but still unable to determine if I can call a function directly in the script or will need to direct script flow via a select or if statement at the top of functions.php
You’ll need to differentiate in some way. You could have the functions at different locations, or if they have different numbers of arguments you could use that.
Or if there is anything about the args they accept that makes each unique and distinguishable from the others that would work too.
Just to make it clear, there is no way to directly call a PHP function from Javascript. You’ll need to find a way to differentiate between different PHP functions, if they are all contained within the same PHP file.
Of course, you could have several PHP files that each contain only the function required (plus any supporting code), and save them as the function names. That way your JQuery code calls the function by PHP file name (passing any relevant arguments). 