Page 1 of 1

Form Validation

Posted: Mon Nov 05, 2007 6:56 am
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

Posted: Mon Nov 05, 2007 7:01 am
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>

Posted: Mon Nov 05, 2007 7:33 am
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