Adding error message to form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Adding error message to form

Post by tomsace »

Hey,

I have a registration form and I can't get error messages to show on the same page as the form. Errors such as 'first name empty' when the field is blank.

I have codes such as:

Code: Select all

if (strlen($_POST['name']) < 5)
   {
    die ("Incorrect email. Please enter valid email address..");
    }
Which writes the error in the page as if the page loads up to the error message, then stops loading and it looks very ugly.

I have tried changing the code in the 2 following ways:

Code: Select all

if (strlen($_POST['name']) < 5)
   {
    echo ("Incorrect email. Please enter valid email address..");
    }

Code: Select all

if (strlen($_POST['name']) < 5)
   {
    $error =  "Incorrect email. Please enter valid email address..";
    }
 
echo ("$error");
When doing both codes its as if the form then ignores the error's and just inserts them into the database as they are...

Can anyone see where I am going wrong?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Adding error message to form

Post by califdon »

1. You are writing PHP code.
2. PHP code is server scripting, it never reaches a browser.
3. That's exactly what or die() is intended to do: if there's an error ... well, DIE, so you can debug it. It can't be used for user prompts.

If you want something to happen in the browser, as a result of bad input, for example, you have to program it in Javascript. Or if you're talking about something that requires checking at the server, then use Ajax and send back an error message to the same page, but it has to be done in Javascript because PHP is all done by the time a page is sent to a browser.
Griven
Forum Contributor
Posts: 165
Joined: Sat May 09, 2009 8:23 pm

Re: Adding error message to form

Post by Griven »

Javascript will check your user's input automatically, and provide instant feedback, providing a richer user experience. However, a malicious user can download your page and strip out your javascript, rendering its validation useless. Also, some users have javascript disabled. Thus, you'll have to double up with server-side validation.

Once your form is submitted, javascript or no, have your PHP double check the validation just to make sure. Session variables can be used to echo error messages from this server-side validation.
achintha
Forum Newbie
Posts: 13
Joined: Mon Apr 30, 2007 10:21 am

Re: Adding error message to form

Post by achintha »

here use this. create a file add.php and put the code.

Code: Select all

<?php
 if(isset($_REQUEST['submit']))
{
    if($_REQUEST['name']!='')
    {
        echo "Your name is ".$_REQUEST['name'];
    }
    else
    header('Location:add.php?msg=Please enter a name');
 
}
else
{
?>
<form action="add.php" method="post">
  
<?php if(isset($_REQUEST['msg']))
 echo"<p>".$_REQUEST['msg']."</p>";?>
  
  <p>Name:
    <input name="name" type="text"  value=""/>
  <input type="submit" name="submit" id="button" value="Submit" />
  </p>
</form>
<?php } ?>
Last edited by Benjamin on Fri May 29, 2009 10:31 am, edited 1 time in total.
Reason: Changed code type from text to php.
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: Adding error message to form

Post by tomsace »

Hey,

Managed to get it working using the method McInfo gave me!
Took a while but figured it out! Thanks alot everyone.

Tom.
Post Reply