View Full Version : Input Button do PHP Function
Jasonb61
08-31-2002, 12:41 AM
Is there a way for a input button to execute a PHP Function...
Like this:
<?php
function Xerror($Errmsg) {
echo $Errmsg;
}
echo "<input type=button name=\"name\" value=\"Value\" OnClick=\"Xerror('HELP')\">";
?>
I know that doesnt work, but maybe it gives you a idea of what I need....
Thanks,
-JohnnyX
Spookster
08-31-2002, 03:57 AM
Using a form element without a form is not wise.
You would need to wrap it in form tags and set the action of the form to the php page you want to execute.
Or use a regular text link.
stuntboy
08-31-2002, 07:37 AM
The page has to reload in order to the function to fire so the best way is as Spookster said to use a link or throw in the form tags. I am assuming that you have a version of php 4.1 or greater. You also probably dont need to have your form out putted with an echo. with php you can jump in and out of php so you can leave less for the server to parse
<?php
function Xerror($Errmsg) {
echo $Errmsg;
}
if(isset($_GET['XerrorCall']))Xerror($_GET['XerrorCall'])
?>
<form action="<?=$_SERVER['SCRIPT_NAME']?>" method="get">
<input type="submit" name="XerrorCall" value="help">
</form>
There is not supposed to be a line break above in the function call for Xerror($_GET['XerrorCall']) I dont know why it is breaking
What does the PHP function actually do? (I assume a simple echo of an error was an example)
Can javascript accomplish what you want? Could you pass php generated data to a js function? eg -
echo '<script>function eror(data) { alert(data); }</script>
<input onclick="eror(\''.$phpvar.'\');">';
?
freakysid
08-31-2002, 12:22 PM
PHP is executed on the server. What you want to do is execute something on the client (web browser). Use Javascript to execute stuff on the client. Otherwise, as has been said, you will need to make a request back to the server to process the user action. (either a hyperlink - GET, or POST form data).
firepages
09-01-2002, 04:01 AM
hello freakysid - good to see you here !
freakysid
09-01-2002, 11:58 AM
Hi firepages. I can't reach sitepointforums.com because of a routing problem - so I have come here for my fix ;)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.