Problem with field validation script

Links for php scripts

Moderators: egami, macek, gesf

Post Reply
User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2192
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

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>';

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 2 guests