Solved

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

Posted on 2007-03-23
2
207 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
ID: 18783042
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
ID: 18783058
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

776 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