multiple forms within a Switch statement

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Ok... I have to make this administation area where I have multiple Contents
to add edit delete publish .
The problem is that I don't know what is the best way of making the forms.
At the moment I create a new PHP file for add_page.php, add_student.php,
add_news.php, edit_page.php list_page.php and so on.

This way doesn't seem very good to me and I am wondering if I want to put
them in one file or in some more generic files, how am I going to do that.

For start created the content.php and did a switch $_GET['category'] and a
nested $_GET['action']  but I don't know what each case: should contain.

I don't want to put the whole html code for the forms in each case....
I have tried puting each form in a function but I had some stupid errors and
that also doesn't sound to me a good solution.

So if someone has previous experience pls Suggest me something...

Re: multiple forms within a Switch statement

To be honest, using a switch for this kind of thing starts off looking
good, but turns into a maintenance nightmare. Also, you want to avoid
pages with modes. Think of your pages much the same way you think of
functions. Each page has a specific purpose: Gather sudent data from
the user, validate / insert student data into the DB, delete a student,

Your first method isn't that bad, to be honest. I would change the
names a little bit, so that they group together in a directory listing:


Need to make a change to the form to add a student? The code you're
looking for becomes easy to find.

(repeats mantra) Simple is good... simple is good...

Re: multiple forms within a Switch statement

Ok thanks to Both of you (dracolytch and alvaro) for your suggestions.
I have just managed to make it by having a content.php with just the switch
statements and each case: in it calling the appropriate Function from a
form_functions.php file.

I am not really  sure what is the best ... but I'll try it both ways !!!
Thanks again I ma still open to other Suggestions !

Re: multiple forms within a Switch statement

*** Angelos wrote/escribió (Tue, 17 May 2005 12:59:09 +0000 (UTC)):
Quoted text here. Click to load it

Keep it simple.

Keep it simple unless you're building a huge site like "". Your
approach looks good to me. It'd only change it if I were duplicating code,
and in the latter case you might solve it moving code to common

-- Álvaro G. Vicario - Burgos, Spain
-- - Mi sitio sobre programación web
-- Don't e-mail me your questions, post them to the group

Re: multiple forms within a Switch statement

Quoted text here. Click to load it

This is the basic schema I use: every GET/POST request should return
two parameters: the name of a function (func) and the argument of that
function (arg). The argument is serialized and URL-encoded.

    function add_page($form)
    /* Show the FORM. $form may be null or may be the data to be
        modified or to be corrected. */
        echo "<html><body><h1>Add page</h1>"
        . "<form method=post action=" . $_SERVER['PHP_SELF'] . ">"
        . "<input type=hidden name=func value=POST_add_page>"
        . "<input type=hidden name=arg  value=>"
        . "<input type=text name=field1 value=" . enc_val($form->field1) .">"
        . "<input type=text name=fieldn value=" . enc_val($form->fieldn) .">"
        . "<input type=submit name=b value=Save>"
        . "</form>"
        . "</body></html>";

    function POST_add_page($arg_not_used)
    /* Take the POST request from add_page() */
        /* Acquire POST into an object: */
        $form->field1 = $_POST['field1'];
        $form->fieldn = $_POST['fieldn'];

        /* Validate all the data: */
        $err = "";
        if( $form->field1 is not valid )
            $err .= "Data1 not valid because so and so...";
        if( $form->field1 is not valid )
            $err .= "Data1 not valid because so and so...";

        if( strlen($err) == 0 ){
            # store data in the DB.
            # show next page - for example another empty form:
        } else {
            echo "<html><body>Invalid: $err<p>"
                . "<a href=$_SERVER['PHP_SELF']?func=add_page&arg="
                . urlencode( serialize( $form ) ) . "RETRY</a>"
                . "</body></html>";

    /* More functions here. */

    /* Request handler: */
    $func = $_REQUEST['func'];
    $arg = $_REQUEST['arg'];
    if( strlen($arg) == 0 )
        $arg = null;
        $arg = unserialize($arg);
    switch( $func ){
    case 'add_page':      add_page($arg);      break;
    case 'POST_add_page': POST_add_page($arg); break;
    # more functions here.
    default: something_maybe_add_page($arg);


All the code may be semplified if you implement these functions;

    function Form($func, $arg)
        # Open the form that send the POST to the function $func with
        # argument $arg; set the hidden fields accordingly.

    function Anchor($text, $func, $arg)
        # Print an anchor to call the function $func with argument $arg
        # and clickable text $text.

As an evolution of these concepts, you may allow two or more buttons in
each form, associating each button to a different function/argument of
the module. This is coerent with the concept that every "click" on your
WEB application may call a different, specific function:

    function Button($text, $func, $arg)

The correctness of the parameters returned by the client may be ensured
adding a MAC (message authentication code) computed over the $func and
the $arg variables. This MAC is generated by the functions Anchor() and
Button() and must be checked by the "request handler" on every request.

Another and even more interesting feature to explore is the introduction
of a stack of $func/$arg values, so to implement the "return" instruction
commonly available on every programming language. The availability of
a stack open the way to the modularization of the WEB applications.

/_|_\  Umberto Salsi

Site Timeline