php 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
Templeton Peck
Forum Commoner
Posts: 45
Joined: Sun May 11, 2003 7:51 pm

php form validation

Post 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 :)
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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

}
?>
Last edited by McGruff on Thu Aug 11, 2005 8:54 am, edited 1 time in total.
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post 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.
User avatar
Templeton Peck
Forum Commoner
Posts: 45
Joined: Sun May 11, 2003 7:51 pm

Post 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?
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

check this

Post 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>
User avatar
Templeton Peck
Forum Commoner
Posts: 45
Joined: Sun May 11, 2003 7:51 pm

Post by Templeton Peck »

Thanks! thats exactly what I needed to see :)
User avatar
Templeton Peck
Forum Commoner
Posts: 45
Joined: Sun May 11, 2003 7:51 pm

Post 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
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

try echo $_POST['start'] maybe something is stored in it.

give us some more code
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post 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.
Post Reply