1

I'm having a problem with one of my PHP form validation scripts.

Basically, I have a form that when a user submits information it does validation checking (via PHP) and outputs the result (Success, Error1, Error2 etc) via an echo statement

However, after a user Submits the form (the action=same page ie. it posts to itself) the validation message appears at the top of the form or next to it. I want the echo message to appear under the form.

This is what is happening now in the page:

Error: Your password is incorrect

Username:

Email:

Password:

Submit Button

This is what I want it to look like:

Username:

Email:

Password:

Submit Button

Error: Your password is incorrect

Does anyone know how to remedy this?

Here is the code:

<form id="username_check" name="username_check" method="post">
<tr>
<td><b>Username:</b></td>
<td><input name="Username" type="text" class="textfield" id="Username" 
value="<?php echo($_POST['Username']); ?>" 
/>
</td>
</tr>
<tr>
<td><b>Email Address:</b></td>
<td><input name="email_address" type="text" class="textfield" id="email_address" 
value="<?php echo($_POST['email_address']); ?>"
/>
</td>
</tr>
<tr>
<td><b>Password:</b></td>
<td><input name="PASSWORD" type="password" class="textfield" id="PASSWORD" />
</td>
</tr>
<td><input type="submit" name="submit" value="Submit" /></td>
</form>
<?php  
//If form was submitted  
if (array_key_exists('submit',$_POST)){ 
//Do something  
echo "Form validation here.....";
}  
?>

4 Answers4

2

This is dependent on the order of execution of the code. If you are using an IDE, set a breakpoint before the validation and step through. Otherwise, I'm pretty sure we'll need to see some of the code to get an idea of why this is happening.

Try:

<form id="username_check" name="username_check" method="post">
<table>
<tr>
<td><b>Username:</b></td>
<td><input name="Username" type="text" class="textfield" id="Username" 
value="<?php echo($_POST['Username']); ?>" 
/>
</td>
</tr>
<tr>
<td><b>Email Address:</b></td>
<td><input name="email_address" type="text" class="textfield" id="email_address" 
value="<?php echo($_POST['email_address']); ?>"
/>
</td>
</tr>
<tr>
<td><b>Password:</b></td>
<td><input name="PASSWORD" type="password" class="textfield" id="PASSWORD" />
</td>
</tr>
<td><input type="submit" name="submit" value="Submit" /></td>
</table>
</form>
<?php  
//If form was submitted  
if (array_key_exists('submit',$_POST)){ 
//Do something  
echo "Form validation here.....";
}  
?>
Jason
  • 45
  • 6
  • If you echo the validation code without the if statement, is it still rendered at the top of the form? I'm assuming the code above is a very boiled down version of what you are doing. Are there any encapsulating div or table tags that could be causing this and remedied with CSS or a few line breaks? – Jason Jan 23 '12 at 17:49
  • 2
    Actually, there is your problem. http://stackoverflow.com/questions/5967564/form-inside-a-table Forms cannot be handled inside a table element. – Jason Jan 23 '12 at 17:51
0

If you are calling the

echo $error_message;

before showing the form, then the message is displayed before the form.

If you want the error message after the form just move the echo command after the form is printed out.

Kypros
  • 2,999
  • 5
  • 19
  • 27
0

Sounds like you are echo'ing out your error message, then echo'ing out the form.

Do it the other way around.

Rik Heywood
  • 13,368
  • 9
  • 56
  • 77
0

Don't echo the error save it in to a variable and print it behind the form print.

That way you can also check every time if an error occur during form validation, simple check if error-variable is emtpy/false

rauschen
  • 3,726
  • 2
  • 11
  • 13