faqts : Computers : Programming : Languages : PHP : Common Problems : Forms and User Input

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

105 of 112 people (94%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

I have a form with 4 buttons, how can I run a different PHP script for each?
Can I run different PHP scripts from the same form submission?
How can I make change the action URL for a form using Javascript?

Jul 5th, 1999 01:34
Nathan Wallace, Frank Kooger, John Coggeshall


A single HTML form can have multiple buttons, but it can only ever have
one action attribute.  That means that every submission from that form
must run that single action URL.

You can run different PHP scripts for different buttons by using
include().

    http://www.php.net/manual/function.include.php3

Just create a single PHP script which contains a conditional statement,
testing for which button was pressed.  You can then just include a
different PHP script for each of the possibilities.

You can do somthing like this:
 
    <FORM ACTION="formproc.php3">
    <INPUT TYPE="submit" NAME="ButtonA" VALUE="Button 1">
    <INPUT TPYE="submit" NAME="ButtonB" VALUE="Button 2">
    </FORM>
 
    formproc.php3
 
    <?php
 
    if(isset($ButtonA)) {
        // code and html for A 
        include("scriptA.php3");
    } 
 
    if(isset($ButtonB)) {
        // code and html for B 
        include("scriptB.php3");
    }
 
    ?>

You can also cause different requests to be sent to the server (ie: the
correct one for each button) using Javascript.  Use Javascript and 4
buttons of type <input type=button> instead of <input type=submit>. Then
you end up with four Javascript functions like:

    function gotopage1()    {
        document.forms[0].action="some.phtm?someparm=somevalue";
        document.master.submit();
    }