How can I keep fields already entered in a form to remain if there was an error

Amanda Watson
Amanda Watson used Ask the Experts™
Hi There,
With my Joomla/Virtuemart registration form, if you enter in all the fields and it turns out that someone has the same username you have chosen, for example, then you get an error message, but all the fields get cleared out so the user has to enter in all the details again.

Can someone help me to keep the variables that have already been entered?

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Hi Tia,

In order to keep persistent forms, you have to populate the input fields with the data inputted in the post.

For example,

 echo 'Name : <input type="text" name="name" value="'.htmlspecialchars($_POST['name']).'"/><br />';  

If the $_POST['name'] variable is set, it will display it again in the error message with this piece of code.
I would suggest the following, to avoid errors
For input,
<input type="text" name="INPUTNAME" value="<?php if(isset($_POST['INPUTNAME'])) echo $_POST['INPUTNAME']?>" />

For textarea
<textarea name="TXTANAME"><?php if(isset($_POST['TXTANAME'])) echo $_POST['TXTANAME']; ?></textarea>

Similar for radio, checkbox.
Replace the UPPERCASE text with what you need.

For select, a little more work is needed because you have to check each value. I'll demonstrate with 2 values

<select name="SLCTNAME">
<option value="1" <?php if(isset($_POST['SLCTNAME']) && $_POST['SLCTNAME']=='1') echo 'selected="selected"'; ?> >value 1</option>
<option value="2" <?php if(isset($_POST['SLCTNAME']) && $_POST['SLCTNAME']=='2') echo 'selected="selected"'; ?> >value 2</option>

If you try any of these in a form that has the action="" set tot the same page (e.g. empty action) it will work.
This is a bit trickier to accomplish if your action goes to a different page
UX Engineer
+1 for @hexer4u recommendation.. for a couple of reasons..
1) It makes the form easier to code since the PHP is only within the 'value' attribute - this also makes adding this to existing forms very easy.
2) using if(isset()) will make sure to check for a value first.. without it, if the code just echo'd the $_POST value.. it would error when empty ;)
3) It really is only the best use for a form that has action="" and not directed to another page, etc..

To implement this into the VM registration form you'll want to edit this file:

Depending upon what version of VM you have.. the changes needed may vary. Can you confirm the Joomla and VM versions you are using??  Also, since the VM fields are created dynamically, this particular method wont work - you will have to modify the foreach() statement within the checkout_register_form.php file to include the value attributes of each field.

YOU CAN, try this approach which has shown some success from other users:

Amanda WatsonWeb Developer


This approach worked beautifully

Thanks for all your help
Jesse MatlockUX Engineer

;) anytime. Glad to be of help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial