Solved

HOW CAN I VALIDATE USING FUNCTION AND RETURN OR REDIRECT IF THE VALIDATION IS SUCCESS? PHP

Posted on 2007-03-23
2
202 Views
Last Modified: 2013-12-13
Hello Experts,

I am very new to PHP and have a question regarding submitting a form after validation is successful.
My questions are:

1. HOW CAN I RETURN ERRORS AND ECHO THEM?
2. HOW CAN I ECHO "VALIDATION SUCCESS", MY CODE DOESN'T WORK PROPERLY)
3. CAN I RETURN $errors LIKE return $errors INSTEAD OF return false?

Any help in the this matter would highly be appreciated.
Lets say I have four fields on my form:
 FIRST NAME  |  LAST NAME  | EMAIL  |  PHONE

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$phone = $_POST['phone'];

So I write this simple function:

$errors = '';

function check_Val($val)
   {
    if(!isset($name))
          {
          $errors = "PLEASE ENTER FIRST NAME";
         return false;
         }
    }

// I CALL THE FUNCTIONS

check_Val($firstname);
check_Val($lastname);
check_Val($email);
check_Val($phone);

   if (isset($_POST['Submit']))
     {
       if ($errors == '')
       {
       echo "VALIDATION SUCCESS";
       }
    }
 
So the form is submitted to itself:

<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
0
Comment
Question by:combustion007
  • 2
2 Comments
 
LVL 1

Accepted Solution

by:
micacca earned 250 total points
Comment Utility
function check_Val($val,$error)
   {
      if ($val == ""){
            $return = "Please enter ".$error;
      }
return $return;
    }

// I CALL THE FUNCTIONS

$error = check_Val($firstname,"First Name");
$error = check_Val($lastname,"Last Name");
$error = check_Val($email,"Email");
$error = check_Val($phone,"Phone");

   if (isset($_POST['Submit']))
     {
       if ($error == '')
       {
       echo "VALIDATION SUCCESS";
       }
         else {
               echo $error;
         }
    }

This is an easy way to do this....

To return errors to have to put them somewhere first.
-> function test() {
  return "hello";
}

$val = test();
or echo test();

in your code you do this "if(!isset($name))" but the variable with the name $name does not exist anywhere in your code so it will always be empty and never get success....

CAN I RETURN $errors LIKE return $errors INSTEAD OF return false?
Exactly as you said return $errors or any other variable that exists in your function..

When you do functions try not to use any other variables that do not exist in the function. If you wish to pass some values into your function put them in the parameters section as you did with your function...

Keep in mind that you cannot access variables outside your function unless you asked them by using global
for example

$var = 1;
function test() {
echo $var;
}
will show nothing
function test() {
global $var;
echo $var;
}
now will show 1
or you can pass it from the parameters
function test($value) {
echo $value;
}
this will work to
0
 
LVL 1

Expert Comment

by:micacca
Comment Utility
Oh and to redirect if everything is ok you do it like this

if (isset($_POST['Submit']))
     {
       if ($error == '')
       {
       header ("Location: yourpagename.php");
       }
         else {
               echo $error;
         }
    }

but to be able to use the header function no other output must be sent to the browser
meaning that no other echo before header should be executed or html code before header for example

<html>
<head> etc etc
<? header (); ?>
this will not work

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now