Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Squeezing the error page - to preserve user's input on error

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Tue Nov 17, 2009 11:41 pm    Post subject: Squeezing the error page - to preserve user's input on error Reply with quote

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:

  1. Show up a blank form to type in.
  2. Registration attempt (submit button clicked).
  3. If successful, go to success page.
  4. If failed, go to error page.
  5. Populate the user's data that s/he filled up already.
  6. 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">&nbsp;</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
View user's profile Send private message Visit poster's website
djkee
Smarty n00b


Joined: 23 Nov 2009
Posts: 3

PostPosted: Mon Nov 23, 2009 3:59 am    Post subject: I like it :) Reply with quote

Thats awesome Very Happy Thank you for sharing
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP