on 28/01/03 10:20 AM, scott ([EMAIL PROTECTED]) wrote:

> hi
> 
> looking for some advice
> 
> I have a site in progress. user to it have to enter a LOT of information (it
> will contain a club directory)
> 
> in order to try and make it a less unpleasant experience for the user, I
> want to give them 7 smaller input forms, each one following the other
>
> what would be the correct way to do this, one very large, complex coded php
> conditional form that does everything, or several smaller ones ?

one script to "run" the form with decent logic to break it up into a few
sections... but that's makes reading/updating the script difficult, so I
prefer to include() the HTML code for each form, and in some cases, eve
include() the main programming for each "section"


> if I use one form, it will conatin rather a lot of code, but execution would
> all be handled by the form itself
> 
> but if I use multiple smaller forms, each form would need to process the
> previous forms input, and then display it's inputs for entry
> 
> I am also using sessions, which adds a little to complexity

actually, they help!!  You can save the contents of form1 into the session
before showing form2, then adding the contents of form2 before showing
form3, etc etc.  Much easier than setting cookies, writing hidden inputs on
subsequent forms, etc etc.

> any general good coding practice tips/links would be helpful

Use $_GET variables to keep track of which part of the form you are upto,
and what you are doing.

I'd suggest this is a lot clearer than reyling on $_POST['submit'] being
true or anything else like that.

So, maybe you have a few status':

- status=showForm
- status=validate


And for both modes, you would tell the script which form part to
show/validate using a GET variable formPart (=1,=2, etc)


So when the script first loads, set a default of status=showForm and a
formPart=1


<?
session_start();


// $status
if(isset($_GET['status'])) { $status = $_GET['status']; }
else { $status = 'showForm'; }

// $formPart
if(isset($_GET['formPart'])) { $formPart = $_GET['formPart']; }
else { $formPart = 1; }
?>


Then BEFORE anything is sent to the browser, validating would usually take
place, so that you can do any redirects or anything like that.  The way I've
set it up, is that each form part has it's own php script with some code to
validate the form entry.

<?
if($status == "validate")
    {
    // look for an include file which matches the current $formPart
    // and include it... haven't put in place an error checking
    $validateCodeFile = "inc/validateFormCode/{$formPart}.php";
    if(file_exists($validateCodeFile))
        {
        include($validateCodeFile);
        }
    }
?>

I'll show a sample of these include files later.  The aim of MY include
files is to return a var $validEntry of true or false... based on that, I
can decide whether to add the form contents to the session / database, or
whether I should show the form again, due to errors.

<?
if($validEntry)
    {
    // either add form contents to session or to database
    //
    //

    // redirect to status=showForm&formPart=[next]
    $next = $formPart + 1;
    header("Location: myscript.php?status=showForm&formPart={$next}");
    }
else
    {
    // show same form again with error message
    $message = "you screwed up";
    header("Location:
myscript.php?status=showForm&formPart={$formPart}&message={$message}");
    }
?>

That (roughly) takes care of validating each form entry, keeping track of
all the vars with sessions or adding to a DB, and passing people onto the
next form part.

Then you'd want a few lines of HTML, then some code to show the right
formPart's HTML code, and a few lines to clode off the HTML.

if($status = 'showForm')
    {
?>
<html>
<head><title>form</title></head>
<body>
<?
    $includeFormCode = "inc/forms/{$formPart}.php";
    if(file_exists($includeFormCode))
        {
        include($includeFormCode);
        }
?>
</body>
</html>
<?
    }
?>

The secret is that we're using include files to make sure the form's code
base isn't messy.


The whole script in one:


---
<?
session_start();

// $status
if(isset($_GET['status'])) { $status = $_GET['status']; }
else { $status = 'showForm'; }

// $formPart
if(isset($_GET['formPart'])) { $formPart = $_GET['formPart']; }
else { $formPart = 1; }



if($status == "validate")
    {
    // look for an include file which matches the current $formPart
    // and include it... haven't put in place an error checking
    $validateCodeFile = "inc/validateFormCode/{$formPart}.php";
    if(file_exists($validateCodeFile))
        {
        include($validateCodeFile);
        }
    
    if($validEntry)
        {
        // either add form contents to session or to database
        //
        //
    
        // redirect to status=showForm&formPart=[next]
        $next = $formPart + 1;
        header("Location: myscript.php?status=showForm&formPart={$next}");
        }
    else
        {
        // show same form again with error message
        $message = "you screwed up";
        header("Location:
myscript.php?status=showForm&formPart={$formPart}&message={$message}");
        }

    }


if($status = 'showForm')
    {
?>
<html>
<head><title>form</title></head>
<body>
<?
    $includeFormCode = "inc/forms/{$formPart}.php";
    if(file_exists($includeFormCode))
        {
        include($includeFormCode);
        }
?>
</body>
</html>
<?
    }
?>
---



As for the include files, you can do whatever you like.  For a 3 part form,
you'd have 6 include files -- 3 for validating code, and 3 for HTML code for
each form.  An example of a simple HTML file would be:

<?
$script = $_SERVER['PHP_SELF'];
?>
<form action="<?=$script?>?status=validate&formPart=<?=$formPart?>"
method="POST">
Name:
<input type='text' name="name" value="" maxlength="50" size="20"><br />
Shoe Size:
<input type='text' name="shoesize" value="" maxlength="2" size="2"><br />
</form>


Then the validate script for this part would be:

<?
$validEntry = 0;
if( (!isset($_POST['name'])) OR (!isset($_POST['shoesize'])) )
    {
    $validEntry = 0;
    }
else
    {
    $validEntry = 1;
    }
?>

... this only makes sure they were both entered... I'm sure you'd want to do
more, but I'm sick of typing :)



That's probably waaaay more than you wanted to think about, but once I
started typing, I found it hard to stop :)

Good luck.


Justin


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to