Page 1 of 1

php form validation

Posted: Mon May 19, 2003 5:43 pm
by Templeton Peck
I simply want to call a php function to validate my form

<form onsubmit = "return validate();" method = "POST" action = "summary.php" >

how do I properly call the php function validate?

thanks :)

Posted: Mon May 19, 2003 6:59 pm
by McGruff
There is no native php function for form validation: you'll have to write one something like:

Code: Select all

<?php
IF (!isset($_POST['var1']) OR !isset($_POST['var2']) OR ..etc ) {

    // back to the form

} ELSE {

    // process form

}
?>

Posted: Mon May 19, 2003 7:26 pm
by AVATAr
Templeton, be aware that php is server side.. so as McGruff told, you have to check the values AFTER submit - checking $_POST[] variable.

If you want to make your validation on the client side you can use javascipt, but the best solution is to use server side validation cause the user may have javascript off.

Posted: Mon May 19, 2003 7:47 pm
by Templeton Peck
thanks guys, I get what your saying

How can I make it go back to the original php page if the information sent is not valid?.. what I want to know is the command to go back to the original page?

check this

Posted: Mon May 19, 2003 8:16 pm
by AVATAr
check this example

Code: Select all

<?php
if (isset($_POST&#1111;'btn_ok']))&#123;
   //OK button pressed
   //Initialize error array
   $errors = array();
   //Check errors
   if (strlen(trim($_POST&#1111;'form_text'])) == 0)&#123;
      $errors&#1111;'form_text'] = 'Please enter text...';
   &#125;
   
   if (count($errors) == 0)&#123;
      //Process Form
      echo "Form processed";
      //redirect after this
      //Header("Location: ");
      //exit;
   &#125;
&#125;
?>

<HTML>
<HEAD>
<title></title>
</HEAD>
<BODY>
<FORM name="SeccionForm" method="post" enctype="multipart-form-data"
      action="<?php echo $_SERVER&#1111;'PHP_SELF']; ?>">
<input name="form_text" type="text" value="<?php echo $form_text; ?>"><?php echo $errors&#1111;'form_text']?></TD>
<INPUT class="button" TYPE=Submit value="OK" name="btn_ok"></TD>
</FORM>
</BODY>
</HTML>

Posted: Mon May 19, 2003 8:21 pm
by Templeton Peck
Thanks! thats exactly what I needed to see :)

Posted: Mon May 19, 2003 9:42 pm
by Templeton Peck
if (isset($_POST['submit']))
{
if (empty($_POST['start']))
{
echo "Blank start date";
header("Location: http://localhost/query.php");
exit;
}


}

can someone please tell me whats wrong with this code, and why it still submits even if there is nothing in start

Posted: Tue May 20, 2003 12:08 am
by AVATAr
try echo $_POST['start'] maybe something is stored in it.

give us some more code

Posted: Tue May 20, 2003 2:16 am
by []InTeR[]
echo "Blank start date";
header("Location: http://localhost/query.php");

You cannot have any output of any kind, before calling a header.

Remove the echo

Read this manual.