Link to home
Start Free TrialLog in
Avatar of Amanda Watson
Amanda WatsonFlag for Australia

asked on

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

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?

TIA
Avatar of jonmchan
jonmchan
Flag of United States of America image

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.
Avatar of hexer4u
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>
</select>

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
ASKER CERTIFIED SOLUTION
Avatar of Jesse Matlock
Jesse Matlock
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Amanda Watson

ASKER

This approach worked beautifully
http://forum.virtuemart.net/index.php?topic=59255.15

Thanks for all your help
;) anytime. Glad to be of help.