Remember form values when validating

I have a typical form with typical input fields and dropdowns, as below.

<input type="text" name="companyName" size="60" value="<?php echo $compName;?>" />

When the form is being validated is there a simple way of collecting the post data on refresh and displaying it back.

Was thinking of something like this -

<input type="text" value="<?php echo isset($_POST['companyName'])?$_POST['companyName']:""; ?>" name="companyName"/>

But didn’t know how to integrate the two and if it would work anyway

<input type="text" name="companyName" value="<?php echo(isset($_SESSION['companyName'])) ? $_SESSION['companyName'] : null; ?>">

Maybe like something like the above…make sure you start session_start(); somewhere above it, usually in a config.php or utilities.inc.php file.

Note: I wouldn’t pass any sensitive data using sessions, such as passwords for example.

Hi Pepster, ah this is where I was going wrong, I have tried various options and obviously not starting the session, cheers for reminding me.

Will give your suggestion a go once I get back into work in the morning, but one thing I had was that this is whats there at the moment

<input type="text" name="companyName" size="60" value="<?php echo $compName;?>" />

So on entering a new contract the user can enter a value and I insert it into the database, that php inside the value as you also would know is for when they want to edit the form and thus the value gets filled from the database, so with your suggestion will I be able still do the edit with the value coming from the database, do you know what I mean.

Im only bringing it up as in the first post I was wondering if I needed to somehow integrate them together -

<input type="text" name="companyName" size="60" value="<?php echo $compName;?>" />

and

<input type="text" name="companyName" value="<?php echo(isset($_SESSION['companyName'])) ? $_SESSION['companyName'] : null; ?>">

I usually use the same form, usually for validation purposes. For example if a person puts in a bad password then the form will be used again with the person’s username, telephone, email address already in the form, so he or she doesn’t have to re-enter again. Is that something what your trying to do?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.