Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Apache/PHP: Cannot make phpinfo() and phpmyadmin work 4 33
Hacked File Timestamps 4 49
PHP: concatenate query 12 33
Before I get too far.. best way to save dates data 4 14
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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…
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…
The viewer will learn how to dynamically set the form action using jQuery.

929 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

12 Experts available now in Live!

Get 1:1 Help Now