Forum Moderators: coopster

Message Too Old, No Replies

Getting a PHP function to work with onclick/onsubmit

Invoking PHP functions using buttons

         

aragornking

9:36 pm on Nov 20, 2003 (gmt 0)

10+ Year Member



Hi. It seems to be much more difficult to invoke a PHP function using onclick or unsubmit than a JavaScript one (for me at least). I was wondering if some kind individual could look at this practice code and indicate how it might be changed to activate my test function "button_phpinfo" using onSubmit or onClick. I failed to get "isset" to work here (my fault most likely). As it stands, phpinfo(INFO_GENERAL) runs automatically on loading the page, rather than waiting for the button event. Also, onClick generates an error, whereas onSubmit does not. Thanks.

############################################
<html>
<head>

<?php
function button_phpinfo() {
phpinfo(INFO_GENERAL);
}
?>

</head><body>

<form name="form1" action="button_php.php" method = "post">
<input name="trial" type="button" value="PHPinfo" onsubmit = "<?button_phpinfo()?>">
</form>

</body></html>

jatar_k

9:47 pm on Nov 20, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



phpinfo(INFO_GENERAL) runs automatically on loading the page

and well it should. It is actually done prior to the browser loading the page.

PHP code is executed on the server before the page is even delivered to the browser. Therefore you can't use a server side language to do anything with out making another request to the server.

So, to not have to make another request to the server, you have to use a client side language that is executed on the client side by the browser. Which is javascript.

What is the behaviour you are trying to build? You will just have to resub the page and have it serve something different.

You want to sub the form and have it show php_info?

too much information

9:48 pm on Nov 20, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What's happening is that the page is loading your "<?button_phpinfo()?>" to fill in the void for the onsubmit and when it does it runs your script.

you should just do a regular 'submit' on the form and handle your script with another page that does whatever you need it to do.

<slow> looks like I was not fast enough </slow>

aragornking

11:03 pm on Nov 20, 2003 (gmt 0)

10+ Year Member



Okay. Shame on me. Thanks for the replies. I am learning all this, hopefully rapidly.

Javascript, to the extent I understand a little of it now, is "beautiful", but I was trying to avoid it in this instance since I would have to pass a value/quantity back to a PHP variable eventually in my main script. I am striking our there as well.

Basically, I wish to submit two values (using different onclick buttons) to a function using a simple "onclick" approach, and have the function pass the value to the PHP variable. Depending on the value this receiving PHP variable receives, my script will, or will not, output error messages to a person filling out a guestbook I am constructing (or to be honest, modifying). Sounds simple if I could get a JS variable to talk to a PHP variable. I am trying out a suggestion from another forum member based on an invisible image approach to go from from JS to PHP, but I thought I could take the easy way out and confine myself to PHP all the way.