I have a registration script (called "script.php") divided in 3 steps; this is the basic structure (i have stripped things out like sanitizing user input and preventing direct access to other steps than 1):
<?php
$step = $_GET['step'];
switch($step) {
case 1:
//show form - action="script.php?step=2" method="post"
break;
case 2:
//if user data is good show an overview of the data and show a button to go to step 3 (the button is enclosed in a form - action="script.php?step=3" and method="post")
//if not, show again form - action="script.php?step=2" method="post" - and display errors
break;
case 3:
//add user data into db
break;
}
?>
Real code:
<?php
switch ($step) {
case 1:
//display form
$html = <<<FORM
<form action="/install?step=2" method="post">
<input type="text" name="username">
<input type="email" name="email">
<input type="password" name="password">
<input type="password" name="password_repeat">
<button class="next" type="submit">Next</button>
</form>
FORM;
break;
case 2:
$errors = array();
if (empty($_POST['username'])||!preg_match_all('/^([A-Za-z0-9]+){1,16}$/', $_POST['username'])) {
$errors[] = 'bad/empty username';
}
if (empty($_POST['email'])||!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errors[] = 'bad/empty email';
}
if (empty($_POST['password'])) {
$errors[] = 'empty password';
}
if (empty($_POST['password_repeat'])) {
$errors[] = 'empty password confirm';
}
if ((!empty($_POST['password'])&&!empty($_POST['password_repeat']))&&$_POST['password']!==$_POST['password_repeat']) {
$errors[] = 'passwords do not match';
}
if(count($errors)>0) {
$error_html = 'some errors occurred';
foreach ($errors as $err) {
$error_html .= 'error: '.$err;
}
$form_html = <<<FORM
<form action="/install?step=2" method="post">
<input type="text" name="username" value="{$_POST['username']}">
<input type="email" name="email" id="email" value="{$_POST['email']}">
<input type="password" name="password">
<input type="password" name="password_repeat">
<button class="next" type="submit">Next</button>
</form>
FORM;
$html = $error_html.$form_html;
}
else {
$ent = 'htmlentities';
$html = <<<DATA_OK
<h3>Data overview</h3>
<table>
<tr>
<th>Username:</th>
<td>{$ent($_POST['username'])}</td>
</tr>
<tr>
<th>email:</th>
<td>{$ent($_POST['email'])}</td>
</tr>
<tr>
<th>Password:</th>
<td>{$ent($_POST['password'])}</td>
</tr>
</table>
<form action="/install?step=3" method="post">
<button class="next" type="submit">Avanti</button>
</form>
DATA_OK;
}
break;
case 3:
//connect to db and insert data
break;
}
?>
<!doctype HTML>
<html>
<head>
<title>Script</title>
<meta charset="utf-8">
</head>
<body>
<?php echo $html; ?>
</body>
</html>
The problem is that when i go to step 3 $_POST is always empty. Is the button shown in step 2 (if user data is good) overwriting $_POST? Or is it emptied because that form has no input but only a submit? How can i pass the $_POST data to step 3 without using hidden fields (as they would contain passwords/pw hashes)?
I have searched on google and here on SO but i couldn't find anything related to my problem.