/    Sign up×
Community /Pin to ProfileBookmark

Execute a php function from HTML code in the same program

How do you execute a php function from HTML code in the same site?

[code=php]<?php
function fn1 {

}
?>

<input type=”submit” name=”Name1″ value=”submit” />[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 28.2014 — [code=php]
<?php
function foo()
{
return "fubar";
}
?>
<h1>Example</h1>
<p><?php echo foo(); ?></p>
[/code]
Copy linkTweet thisAlerts:
@PhilosophaieauthorMay 28.2014 — What if you want button functioning from a button? Send it to JavaScript?

[CODE]<?php
function fn1() {
...
Return value1
}
function fn2() {
...
Return value2
}
?>
<script type="text/javascript">
function javascriptbtn(){
\execute fn1
window.document.myform.text1.value = value1;

\execute fn2
window.document.myform.text2.value = value2;
}
</script>
<input type="button" name="Name1" value="Execute php fn1 and fn2" onclick="javascriptbtn()" />
[/CODE]
Copy linkTweet thisAlerts:
@Sup3rkirbyMay 28.2014 — The most important question here is, what exactly are you trying to do with this?

Since PHP is a server-side language, once the page has finished loading all PHP code becomes inaccessible. Thus you cannot call a PHP function from HTML, Javascript or other client-side languages. Via AJAX, javascript would be able to call a PHP script that could then execute various PHP functions, but this isn't something that occurs within a single page/file.

But again, this comes down to what you are actually trying to do. If the function you wish to execute can be converted into a javascript function instead then this would be the way to go, rather than trying to run a PHP function outside of PHP. But if this PHP function has specific actions that require PHP, then it might be suggested to set up a way to execute it via AJAX, which will require you to slightly adapt how this function works and how it affects your page (since it could not be called and executed within the same page).
×

Success!

Help @Philosophaie 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.15,
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,
)...