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
Solved

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

Posted on 2007-03-23
2
212 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

807 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