Simple multipart form
Posted: Thu Jan 04, 2007 2:42 pm
Below a (hopefully) simple piece of code to process a multi part form.
sessiontest.php
This is something I just coded, thinking about how I could write a simple one-page multi part form. So, do you think:
a) after 14 hrs behind a screen you're wasted, go and do something else
b) load of crap, start over
c) you're too lazy ***, think for yourself,
d) well, I have a suggestion ...
My main question concerns the multi part aspect of this. I used sessions, seemed easy for me, but other methods are available.
[EDIT:] Ok, after a few hours sleep I already see that it is a load of crap with a lot of redundant code. I'll review and repost the improved code later today.
sessiontest.php
Code: Select all
<?php
$message = '';
$clean = array();
$html = array();
$sql = array();
session_start();
// process step 1
if( isset($_POST['posted']) && $_POST['posted'] == "1" && $_SESSION['formstep'] == 1 )
{
// CHECK Token
if (isset($_SESSION['token']) && $_POST['token'] == $_SESSION['token'])
{
/* Valid Token */
$message = 'Step 1 has been processed succesfully';
// include (templates/formtemplate2.tpl.php);
// do processing ...
}
// increase session step
$_SESSION['formstep'] = 2;
}
// process step 2
elseif( isset($_POST['posted']) && $_POST['posted'] == "1" && $_SESSION['formstep'] == 2 )
{
// CHECK Token
if (isset($_SESSION['token']) && $_POST['token'] == $_SESSION['token'])
{
/* Valid Token */
$message = 'Step 2 has been processed succesfully';
// include (templates/formtemplate3.tpl.php);
// do processing ...
}
// set step to 3
$_SESSION['formstep'] = 3;
}
// process step 3
elseif( isset($_POST['posted']) && $_POST['posted'] == "1" && $_SESSION['formstep'] == 3 )
{
// CHECK Token
if (isset($_SESSION['token']) && $_POST['token'] == $_SESSION['token'])
{
/* Valid Token */
$message = 'Step 3 has been processed succesfully';
// include (templates/thankyoupage.php);
// do processing ...
}
// set back session step to 0
$_SESSION['formstep'] = 0;
}
else
{
$message = 'This is the first step of the form process';
$_SESSION['formstep'] = 1;
// include (templates/formtemplate1.tpl.php);
}
$token = md5(uniqid(rand(), TRUE));
$_SESSION['token'] = $token;
$_SESSION['token_time'] = time();
?>
<html>
<body>
<p><?php echo 'This is step: ' . $_SESSION['formstep']; // obviously this is just for demo's sake ?></p>
<p><?php echo $message; // also ?></p>
<p><?php echo 'The secret token is: ' . $token ; // also ?>
<form action="sessiontest.php" method="post">
<fieldset>
<legend>Form</legend>
<input type="hidden" name="posted" value="1">
<input type="hidden" name="token" value="<?php echo $token ?>" />
of course I will change the part below to include different forms
<div>
<label for="firstname">firstname:</label>
<input type="text" value="<?php echo isset($html['firstname']) ? $html['firstname'] : ''; ?>" name="firstname" id="firstname" />
</div>
<div>
<label for="lastname">lastname:</label>
<input type="text" value="<?php echo isset($html['lastname']) ? $html['lastname'] : '' ?>" name="lastname" id="lastname" />
</div>
</fieldset>
<input type="submit" value="order">
</form>
</body>
</html>a) after 14 hrs behind a screen you're wasted, go and do something else
b) load of crap, start over
c) you're too lazy ***, think for yourself,
d) well, I have a suggestion ...
My main question concerns the multi part aspect of this. I used sessions, seemed easy for me, but other methods are available.
[EDIT:] Ok, after a few hours sleep I already see that it is a load of crap with a lot of redundant code. I'll review and repost the improved code later today.