Solved

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

Posted on 2007-03-23
2
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

724 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