/    Sign up×
Community /Pin to ProfileBookmark

PHP functions called by buttons.

Ok, I need to know how to call a function by pressing a button. Its an img tag that calls a function that includes a php file (its like a link that only loads one thing, i didnt use frames because theyre old school and dont render on all browsers.) So far I used this (fruitlessly):

[code=php]
<?php
function home()
{
include(“home.php”);
}
?>
[/code]

And then this html code (dont mind anything but the onclick, the rest is for a rollover.):

[code=html]
<img
src=”index_files/home_out.gif” style=”width: 128px; height: 25px;
border: none;” onmouseout=”button_off(‘eg’); return true”
onmouseover=”button_on(‘eg’); return true” alt=”home” id=”eg” onClick=”home()”/>
[/code]

I dont really know if thats the right way to call a function in php. Help?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 03.2009 — Since PHP runs on the server, not the browser, you cannot directly call a PHP function from an onclick event. You [i]could[/i] call a JavaScript function that is included with the [X]HTML page, and have that JS function implement an AJAX call to a PHP script that would then execute the desired PHP function. Google on something like "PHP AJAX tutorials" to find all sorts of info.
Copy linkTweet thisAlerts:
@Not_to_mentionauthorMay 03.2009 — Thanks! Ill keep you posted!?
Copy linkTweet thisAlerts:
@Not_to_mentionauthorMay 03.2009 — Im confused... can javascript include files maybe? or maybe another way to trigger the code?
Copy linkTweet thisAlerts:
@MindzaiMay 03.2009 — No, as NogDog has said PHP executes server side, and javascript executes client side. PHP has long since left the picture before javascript shows up. The only way to execute PHP code triggered by an event on the client side is to send another request to the server. You can do this with javascript.
Copy linkTweet thisAlerts:
@Not_to_mentionauthorMay 03.2009 — I guess this will work. I put all the text in home.php into a div with the id "home".

[CODE]
<script language="javascript" type="text/javascript">
function getHomeText() {
var text = document.getElementById("home").value;
var url = "home.php";
}
</script>
[/CODE]


tell me if im missing something.
Copy linkTweet thisAlerts:
@Not_to_mentionauthorMay 03.2009 — It worked! thanks for the help, would never have guessed ajax!
×

Success!

Help @Not_to_mention 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...