using JavaScript to call a php function.


#1

Basically I have some code which needs to be executed server side, I’ve negated it from this and used a simpler example, as it isn’t necessary for you to traul through my server side code.

I want to have a button execute a php function server side, which, you cant do, so I had the button execute some JavaScript to execute the php function as below; im looking to create a generic javascript function with i can use to execute a generic php function like onclick=javascriptfunction(phpfunction)

The script below has no problems calling the php function which then prints hello onscreen (tested),

button calls echoHello() from the script > echoHello() calls the php function hello()

[code]<?php
$a=“hello”;
function hello()
{
global $a;
echo $a;
}
?>

Say Hello[/code]

however;

Can anyone tell me why this code fails, I want to use the code exactly as above but be able to define the php function I wish to call

button calls echoHello(hello) > script should run with $varialble = hello , but it doesn’t or am I doing something wrong.

[code]<?php
$a=“hello”;
function hello()
{
global $a;
echo $a;
}
?>

<button onclick=“echoHello(“hello”)”>Say Hello</butto
n>[/code]


#2

because you’re confusing the script.

you already have $a defined in the php function, so its reading that. And you’re not passing $variable to the php function. not really sure if you can pass a js variable to a php function. I know you can do it the other way around in some cases.


#3

Thanks, I realised that the code isn’t actually calling the php function, its just running it and writing the output. which doesn’t actually execute the code server side.

I’ve changed to this approach

[php]<?php
if (isset($_POST[“control”])){
print(“IS SET”);
}else{
print(“UNSET”);
}
print("\n");
print("\n");
print("\n");
print("\n");
print("\n");
?>[/php]

Its kind of ugly but it uses a form to POST the variable which is read by the php script


#4

UPDATE!

function postFunction() {
$postF = ($_POST[“control”]);
return $postF();
}

function GPIOX() {
print(“THIS IS FUNCTION GPIOX”);
}

function GPIOY() {
print(“THIS IS FUNCTION GPIOY”);
}

if (isset($_POST[“control”])){
postFunction();
}
print("\n");
print("\n");
print("\n");
print("\n");
print("\n");
print("\n");
?>

This is a way I found to call any php function from a html form; just set name=control value=FUNCTION

when you click it it will execute the php FUNCTION serverside


#5

apologies … forgot yo use the

[php]function postFunction() {
$postF = ($_POST[“control”]);
return $postF();
}

function GPIOX() {
print(“THIS IS FUNCTION GPIOX”);
}

function GPIOY() {
print(“THIS IS FUNCTION GPIOY”);
}

if (isset($_POST[“control”])){
postFunction();
}
print("\n");
print("\n");
print("\n");
print("\n");
print("\n");
print("\n");
?>[/php]

php code box


#6

is there a way for my to call a function within a function without using return

i.e

just wrote this to simplify my request a little you can see how i’ve used this in the above post

return ends the function so I cant call 2 functions from within the function.

[php]
function function1(){
print(“hello”)
}

function function2(){
return function1()
}

function2()
[/php]

[php]
function function0(){
print(“say”)
}

function function1(){
print(“hello”)
}

function function2(){
return function1() #this example stops after this function, function0 is not called
return function0()
}

function2()
[/php]


#7

Why so much code? Just execute the PHP page from Javascript. Pass any info you need with arguments.

Even better, just use a hidden iFrame and load your PHP page into it by a button and then your PHP code will execute and you are all set. The PHP page can be anything you need to process. Then, you can display it, or,
you can copy the fields it posts into visible fields. Works well and is quite easy to do.

Just an iframe, a button pointing to a small onclick-JS script that loads the iframe. Much easier than all that code you wrote to execute the PHP server-side… (I use this trick to load all kinds of database data and use it for processing on the fly whenever needed client-side.) Still secure because all database activities are still server-side. Anyways, just an idea for you… Good luck…