/    Sign up×
Community /Pin to ProfileBookmark

calling function in php from html?

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

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@dreamcatcherNov 30.2003 — 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?
Copy linkTweet thisAlerts:
@pyroNov 30.2003 — 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

?
Copy linkTweet thisAlerts:
@jasnjohnauthorDec 01.2003 — 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.
Copy linkTweet thisAlerts:
@pyroDec 01.2003 — Directly, no. Indirectly, yes.

You can do something like this:

[code=php]if (isset($_GET['action']) && $_GET['action'] == "increment") {
#run the function
}[/code]

And to get that to run, you just pass a query string:

<a href="page.php?action=increment">increment</a>
Copy linkTweet thisAlerts:
@jasnjohnauthorDec 01.2003 — 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.?
Copy linkTweet thisAlerts:
@pyroDec 01.2003 — Ok, sounds like a plan. Good luck. ?
Copy linkTweet thisAlerts:
@jasnjohnauthorDec 01.2003 — 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
Copy linkTweet thisAlerts:
@pyroDec 01.2003 — PHP runs server-side, so you will have to send a request to the server for it to run. Take a look at this:

[code=php]<?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;
?> [/code]
Copy linkTweet thisAlerts:
@jasnjohnauthorDec 01.2003 — Beautiful.

Works like a charm.

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

Regards,

John
Copy linkTweet thisAlerts:
@pyroDec 01.2003 — Happy to help. ?
×

Success!

Help @jasnjohn spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...