Page 1 of 1

Adding error message to form

Posted: Tue May 26, 2009 1:17 pm
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?

Re: Adding error message to form

Posted: Tue May 26, 2009 7:18 pm
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.

Re: Adding error message to form

Posted: Tue May 26, 2009 8:36 pm
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.

Re: Adding error message to form

Posted: Fri May 29, 2009 4:27 am
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 } ?>

Re: Adding error message to form

Posted: Fri May 29, 2009 4:40 pm
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.