Board index   FAQ   Search  
Register  Login
Board index PHP PHP Scripts

Problem with field validation script

Links for php scripts

Moderators: macek, egami, gesf

Problem with field validation script

Postby pcfixerjeff » Tue Mar 27, 2012 8:02 pm

Hi this has been driving me nuts for days.

I have a form posting data to the script below and the part i have posted is validating the email and name fields.

I have played with it a bit and the function checkOK is always returning the value of the last if statement.

I have tried swaping the staments round and it is always the last one it returns the value from. has me stumpted.

Basical the function 1st check the name field is not empty if it is it returns a error code of 1 then it checks the email field and if the email does not match the regual expression or it is empty it returns and error code of 2 then it checks against both the user name and email address and if the are both wrong it returns an error code of 3. Im not entirly sure the syntax is correct any more as the function will always return the last value even if i swap around what each if statment is checking for.

PLEASE HELP

<?php
include ('config.php');
include ('global.php');

$ID = '';
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$phone2 = $_POST['phone 2'];
$street = $_POST['street'];
$city = $_POST['city'];
$country = $_POST['country'];
$createdate = date("Y-m-d");
$result = checkOK();

function checkOK(){

if ($name == null){
$errcode = '1';
}

if ((!eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]",$email)) || ($email == null)){
$errcode = '2';
}

if (($name == null) && ((!eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]",$email)) || ($email == null))){
$errcode = '3';
}

else{
$errcode = null;
}

return($errcode);
}

if ($result != null){

header('Location: dashboard.php?p=addclient&success=no&error='. $result);

}
pcfixerjeff
New php-forum User
New php-forum User
 
Posts: 2
Joined: Tue Mar 27, 2012 7:52 pm

Re: Problem with field validation script

Postby egami » Thu Mar 29, 2012 4:43 am

Variables outside of a function that are not considered "Super Globals" do not get passed to the function.

ie.

$name = $_POST['name'];

function checkOK() {
if ($name == NULL) { // it's not even defined.. so this will throw an error.
echo "The name is blank.";
}

return $name; // this was not defined, so the function returns 0
}


So if you want a function to iterate through your $_POST (which is a Super Global), then you do it like this.


function CheckMe($array) {
foreach($array as $key => $value) {
if ($value == '' || $value == NULL) {
$out['error'][$key] = "$key is NULL value.";
}

return $out;
}


$showMe = CheckMe($_POST);

echo '<pre>'; print_r($showMe); echo '</pre>';
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT


Return to PHP Scripts

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.