|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
bimal Smarty Elite
Joined: 19 Apr 2007 Posts: 423
|
Posted: Tue Nov 17, 2009 11:41 pm Post subject: Squeezing the error page - to preserve user's input on error |
|
|
Here, I am letting you know how we can reduce number of pages in a website, by reusing a form. Take a case of membership registration. Below are the sequences of files and actions we need to trap:
- Show up a blank form to type in.
- Registration attempt (submit button clicked).
- If successful, go to success page.
- If failed, go to error page.
- Populate the user's data that s/he filled up already.
- Redo the whole process.
Now, let me explain, how I plan to remove the error page, in this case. While in the main registration page, I try to print a non existing Smarty array variable, named $register. As Smarty does not give out an errror message in this case, the below code gives a valid blank form to allow to register (type user data).
Code: | <form name="register-form" id="register-form" method="post" action="?">
<table border="0" cellspacing="0" cellpadding="0" class="data">
<tr>
<td class="ask">Name:</td>
<td><input type="text" name="register[name]" value="{$register.name}" class="input" /></td>
</tr>
<tr>
<td class="ask">Email:</td>
<td><input type="text" name="register[email]" value="{$register.email}" class="input" /></td>
</tr>
<tr>
<td class="ask">Password:</td>
<td><input type="password" name="register[password]" value="{$register.password}" class="input" /></td>
</tr>
<tr>
<td class="ask"> </td>
<td><input type="submit" name="register-action" value="Register" class="submit" /></td>
</tr>
</table>
</form> |
Do not worry here, just because we have NOT defined $register. It works to show a blank form.
Its handler would look like:
Code: | <?php
if($_POST['register-action'])
{
if($member_id=$member->register($_POST['register']))
{
redirect('registration-successful.php');
# Page stops executing now onwards
}
else
{
$smarty->assign('error', 'Email exists already'); # add reason why it failed...
$smarty->assign('register', $_POST['register']); # And continue to show error message
# Page DOES NOT stop executing.
# It continues to ask the user input again.
# and, preserves the last inputs.
}
}
?> |
This is the same page, but is loaded only after the user clicks on submit button. And, let us assume, the code: if($member_id=$member->register($_POST['register'])) failed to add this user, probably because the email existed already.
Right after this tracking, DO NOT redirect to the error page. But let the page continue. And, this time, we have assigned an error message and a copy of $_POST['register'] into $register for Smarty.
It will preserve the user's last input, and populate the registration form itself. Do not just build another error page in this case.
Helpful? |
|
Back to top |
|
djkee Smarty n00b
Joined: 23 Nov 2009 Posts: 3
|
Posted: Mon Nov 23, 2009 3:59 am Post subject: I like it :) |
|
|
Thats awesome Thank you for sharing |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|