Exec a PHP function from a Form button?

I need to execute a custom PHP function when I click a button (not a type Submit) in a form. I’d also like this function to reside in the same PHP file as the form. I want to avoid as much JavaScript as it is possible.

Could any one point me in the right direction?

TIA, Dan

<input type=‘<? doFunction(); ?>’>

? dno just a guess :wink:

Hi Dan.
I guess you have to use a submit button.
According to the manual you have three types of buttons. And if you don’t want to use a submit button, I guess you need some sort of a client side script:

Authors may create three types of buttons: [list]
[]submit buttons: When activated, a submit button submits a form. A form may contain more than one submit button.
]reset buttons: When activated, a reset button resets all controls to their initial values.
[*]push buttons: Push buttons have no default behavior. Each push button may have client-side scripts associated with the element’s event attributes. When an event occurs (e.g., the user presses the button, releases it, etc.), the associated script is triggered. [/list]

Using a submit, this will work (but it’s a seperate form):


function doSomething() {
    echo 'Guess this is working';

if(count($_POST) > 0 && isset($_POST['execfunction'])) {

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <button type="submit" name="execfunction">Execute</button>

This will cause you to loose all the data that is already entered by the user. That can be solved aswell. :slight_smile:
If you let the ‘Execute’ buttone work on the same form and have it as an submit button. Then you just execute the fuction when the ‘Execute’ button is pushed and refill the form with the already entered data (by the user). If the user pushes the ‘Submit’ button, which is also a submit button, the form wil be submitted as normally.

If you need a code example, I’ll try to write one for you.
Hope this helps. :slight_smile:


Resident Evil, PHP is serverside!

Anyway, you can have more than one submit button, just give them different names.

For example:

<!-- ... -->
<form method="post" action="form.php">
    <input type="submit" name="doFoo" value="Do Foo!" />
    <input type="submit" name="doBar" value="Do Bar!" />

function doFoo()
    echo 'We did foo!';

function doBar()
    echo 'We did bar!';

$functions = array('doFoo',  // Executable function list

// Look for known executes
foreach ($_POST as $name => $post) {
    if (in_array($name, $functions)) {
        if (function_exists($functions[$name]))

Note that someone could submit more than one entry out of the $functions list.

Thank you very much gentlemen.

For me, this is a lot of wonderful new stuff about handeling form with PHP. Give me some time to digest and try this out. Will be back with my results (… and probably more questions… :slight_smile:

Thanks again, Dan

It never occured to me that a Form could have more than one Submit button. Obviously, I still have a long way to go…

Your input helped me implement the Preview option on this test page:
I could keep everything in the same .php file and it works like a charm.

Since I have your attention, would it be possible to open the Preview in a new browser window passing it the code held in the editing textarea?

Thanks, Dan

Thanks. This is an elegant way to handle many Submits (which I didn’t know was possible…). I’ll use your code in the final version of the following test page (when the Post button will post):

By the way, I think it should be “function_exists($name)” and “call_user_func($name);” instead of “function_exists($functions[$name])” and “call_user_func($functions[$name]);”.

Thanks again, Dan

By the way, I think it should be “function_exists($name)” and “call_user_func($name);” instead of “function_exists($functions[$name])” and “call_user_func($functions[$name]);”.

Yes, of course, my earlier code had a different $functions array with indices, so that you could have a submit button called ‘prev’ which would call the function doPreview array(‘prev’ => ‘doPreview’), I dropped it though and forgot to alter the code. Thanks for telling, it’s always good to know that people actually read and understand the code. :slight_smile:

I Do My Submits Like This

<input type='hidden' name='DoSomething' value='true'>
<input type='submit' name='submit' value='Post It'>

Then Get Em, By Like

if($DoSomething) {


Dont Know Iff That Helps Much :p Im Just Bored