Form Validation

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
User avatar
phpcoder
Forum Contributor
Posts: 158
Joined: Sat Nov 02, 2002 1:18 pm
Location: Manchester, UK

Form Validation

Post by phpcoder »

Hi,

I got simple form with name , address and other details. I dont want to use javascript for field validation. I want to use PHP for validation. How I should do this. I mean I should have seperate PHP file on the server that check each field and if there is any filed empty then it should present user with a same page indicating missing filed. Or should I do it on same page. Please let me know which is the best way and give me a small example if you can so that can help me.

Thanks
User avatar
seppo0010
Forum Commoner
Posts: 47
Joined: Wed Oct 24, 2007 4:13 pm
Location: Buenos Aires, Argentina

Post by seppo0010 »

The usual thing is to use the same page as action, so you can show the user the form and the errors and a chance to fix it...

Code: Select all

<?php
if (count($_POST) > 0)
{
  if (validation())
  {
    dosomething();
    header("location: everythingok.php"):
    exit;
  }
  else {
    $error = 'Name is required';
  }
}

//....
?>
...
<?php if (isset($error)) echo '<p>' , $error , '</p>'; ?>
<form action='thispage.php' method='post'>
...
</form>
User avatar
phpcoder
Forum Contributor
Posts: 158
Joined: Sat Nov 02, 2002 1:18 pm
Location: Manchester, UK

Post by phpcoder »

seppo0010 wrote:The usual thing is to use the same page as action, so you can show the user the form and the errors and a chance to fix it...

Code: Select all

<?php
if (count($_POST) > 0)
{
  if (validation())
  {
    dosomething();
    header("location: everythingok.php"):
    exit;
  }
  else {
    $error = 'Name is required';
  }
}

//....
?>
...
<?php if (isset($error)) echo '<p>' , $error , '</p>'; ?>
<form action='thispage.php' method='post'>
...
</form>


Thanks very much
Post Reply