Click to See Complete Forum and Search --> : calling function in php from html?


jasnjohn
11-29-2003, 11:00 PM
Is there anyway to call a function in a php script that is embedded in the html document, from the html part itself?

I'm trying to improve on my Javascript image gallery by doing it in php instead, allowing a bit more dynamic interface (eg, associated text accompanying the image without the need of an unsightly text-box).

I just needed to know how to call a function that increments a variable that supplies the script with info on the image number and the image description text.

Please bare in mind I've only been learning php for about half a day and this is my first attempt.

Thanks,
John

dreamcatcher
11-30-2003, 11:26 AM
Hi jasnjohn,

You can include as much PHP as you want to in your HTML files, including functions, providing that you always use <? ?> to seperate the PHP code and you rename your file with a .php extension. (I believe it is also possible to edit the .htaccess file to make your HTML pages behave like PHP pages).

So to call a function you would use:

<? functionName(); ?>

Does that help in any way?

pyro
11-30-2003, 01:30 PM
It is not recommended that one uses the short tags (ie <?) in production environments, as they can be disabled in the php.ini file. I would recommend always using <?PHP

:)

jasnjohn
11-30-2003, 08:04 PM
Thanks guys,

I guess I hadn't made my question all that clear.

Say for example I had some php code as follows:

<?php
function numinc() {
$num = $num + 1;
}

Is there anyway to call numinc() from my html code?
Say, from the click of a button, or <a href.....?

Thanks, and apologies if it's a dumb question.

pyro
11-30-2003, 08:08 PM
Directly, no. Indirectly, yes.

You can do something like this:

if (isset($_GET['action']) && $_GET['action'] == "increment") {
#run the function
}
And to get that to run, you just pass a query string:

<a href="page.php?action=increment">increment</a>

jasnjohn
11-30-2003, 08:14 PM
As Mr Burns would say with his fingertips placed together....

"Excellent".

Thanks for your help, I'll have to give it a shot later. At work now.:(

pyro
11-30-2003, 08:28 PM
Ok, sounds like a plan. Good luck. :)

jasnjohn
12-01-2003, 05:27 AM
I can't help but think I'm doing something wrong here.

I'm trying to initiate a variable in php that will be incremented via HTML (a href), but am somehow not getting it.

Maybe I'm expecting more of php than it can achieve. I'm hoping that the display will reflect the updated variable without having to refresh the screen.
Am I nuts or is that possible?

Here's my code:

<?php
$num = 1;

function numinc() {
$num = $num + 1;
}



if (isset ($_GET['action']) && $_GET['action'] == "increment") {
numinc();
}
?>

<a href="incrementer.php?action=increment">increment</a>

<?php
print $num;
?>

Does this look like it should work? I get $num printed out as 1, but it won't increment. Is it because $num is declared and assigned "1", and can't escape this even with the function numinc()?

Any help would be appreciated.
Thanks,
John

pyro
12-01-2003, 07:58 AM
PHP runs server-side, so you will have to send a request to the server for it to run. Take a look at this:

<?php
$num = (isset($_GET['num'])) ? $_GET['num'] : 1;
function numinc() {
global $num;
$num++;
}
if (isset ($_GET['action']) && $_GET['action'] == "increment") {
numinc();
}
?>
<a href="<?PHP echo $_SERVER['PHP_SELF']; ?>?action=increment&amp;num=<?PHP echo $num; ?>">increment</a>
<?php
echo $num;
?>

jasnjohn
12-01-2003, 08:14 AM
Beautiful.
Works like a charm.

Thanks for taking the trouble to investigate, it's greatly appreciated.

Regards,
John

pyro
12-01-2003, 08:18 AM
Happy to help. :)