Jump to content

Retain form field information after PHP verification


binarylime

Recommended Posts

Alright guys,

 

I'm having a problem with forms. When a user registers, the form is processed by a PHP script. If there is a problem with the form (passwords don't match, username already taken...), the user is sent back to the form with an error message using require_once('registerform.php').

 

When users are sent back to the form, all their original data is gone. It's annoying for users if their passwords don't match, and they are thrown back to the form and have to fill everything in from scratch.

 

How can i retain the users data in the form fields?

 

Thanks in advance.

No, i just have a standard form which is received my my php processing script which uses several if statements as follows:

 

if (empty($_POST['username']) || empty($_POST['password']) || empty($_POST['confirmpass']) || empty($_POST['firstname']) || empty($_POST['lastname']) || empty($_POST['email']) || empty($_POST['institutename']))

    {

    $warningmessage = "One or more fields missing.";

    require_once('registerform.php');

    exit;

    }

 

 

No, i just have a standard form which is received my my php processing script which uses several if statements as follows:

 

if (empty($_POST['username']) || empty($_POST['password']) || empty($_POST['confirmpass']) || empty($_POST['firstname']) || empty($_POST['lastname']) || empty($_POST['email']) || empty($_POST['institutename']))

    {

    $warningmessage = "One or more fields missing.";

    require_once('registerform.php');

    exit;

    }

 

 

 

 

Do one thing here like store the Post data in the variables

e.g. $username=$_POST['username'];

and so on

 

and use these variables in your "registerform.php" to assign to the appropriate fields

e.g. <input type='text' name='username' value='<?php echo $username; ?>'>

 

etc

 

Cheers ! :)

pdkv2's solution was entirely fine, and will work great. But for the sake of the cleanest output possible, I personally prefer to write it this way:

 

<input type="text" name="username"<?php if(isset($_POST['username']))  { echo ' value="{$_POST['username']}"'; ?> />

 

The reasoning behind this is that in the the original explanation, if the form has not been submitted, the tag will look like this:

 

<input type=2text" name="username value="" />

 

Whereas if done my way, the 'value' is only included if necessary.

 

Its really not that important, and will work either way, but for the sake of clean code...

No worries!

 

Ya, I think its not valid XHTML if there is no value included. For most of the posters on the site, thats not a big issue, but valid semantic XHTML (or at least somewhat semantic) is something I put a lot of effort into writing.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.