Forum Moderators: coopster

Message Too Old, No Replies

Stop form field clearing after error?

         

Beano

8:18 pm on Oct 16, 2011 (gmt 0)

10+ Year Member



Hi everybody,

I have a really annoying problem on my site. I currently have a field for an "Add Listing" page and it automatically generates a "Ref ID" from the database and shows it as a frozen field. It's written like this...

$form->field('frozen', $lng['user']['RefID'], 'RefID', $formSubmit['RefID'], '', '', false, '25', '255');


The problem is, if the user creates an error and has to redo the form, the field clears automatically because the field is frozen, the listing is submitted without a Ref ID.

Is there anyway I can stop this field from clearing? Or better still, just presenting the Ref ID as text above the form?

Thanks everyone,

Simon :)

Status_203

8:13 am on Oct 17, 2011 (gmt 0)

10+ Year Member



Do you reshow the form automatically if there is an error, or does the user have to go back to the form?

Beano

11:11 am on Oct 17, 2011 (gmt 0)

10+ Year Member



Hi Status_203,

I reshow the form automatically with an error message at the top of the page.

Thanks for your help,

Simon

Status_203

7:52 am on Oct 18, 2011 (gmt 0)

10+ Year Member



Then you need to get the submitted value of the field from $_GET or $_POST depending on the form method and use it populate the default value of the form control.

Something along the lines of: (untested)

$defaultValue = '';
if (array_key_exists('field_name', $_GET)) {
$defaultValue = $_GET['field_name'];
}
echo '<input type="text" name="field_name" value="' . htmlspecialchars($defaultValue) . '">";

Beano

1:00 pm on Oct 18, 2011 (gmt 0)

10+ Year Member



Hi Status,

Thanks for your reply! So I'd just put this code within the code I have included further up?

I'm a bit of a novice with this!

Status_203

8:40 am on Oct 19, 2011 (gmt 0)

10+ Year Member




$form->field('frozen', $lng['user']['RefID'], 'RefID', $formSubmit['RefID'], '', '', false, '25', '255');


You appear to be using some form of form generating class. Do you happen to know which one? If not, what files are included/required at the top of the file that includes this line?

If you feel like experimenting it could be that one of the '' parameters is used as the default value - try using the equivalent of $defaultValue from my example in place of one of the empty set of quotes. If it doesn't work replace the empty quotes and try it in place of the other set of quotes.