Page 1 of 1

VAlidate $_POST variables

Posted: Mon Jan 20, 2003 3:52 pm
by kendall
Hi,

im posting cause im trying to find the error in the following code but i just can't see it

function CHECK_EMPTY($_POST){
if(!isset($_POST['First_Name']) && !isset($_POST['Last_Name'])){
$ErrorMsG[] = "Please Enter a Correct Name in the NAME Fields!";
}
if((!isset($_POST['Address1']) || !isset($_POST['Address2'])) && !isset($_POST['City'])){
$ErrorMsG[] = "Please Enter An Address";
}
if(!isset($_POST['Email'])){
$ErrorMsG[] = "Please Enter an Email Address in the EMAIL ADDRESS Field!";
}else{
$ErrorMsG[] = CHECK_EMAIL($_POST['Email']);
}
if(is_array($ErrorMsG)){
while(list($key,$val)=each($ErrorMsG)){
echo $key.",".$val;
}
return $ErrorMsG;
}
}

The above code validates some posted data from a form. The thing is i left some of the form data to test the validation which i get but while it lists the key it doesnt list the value and it isnt looping as i only get one key output.

can any one see wheres the error?

Posted: Tue Jan 21, 2003 2:44 am
by twigletmac
Try this, it works as expected for me:

Code: Select all

<?php
function CHECK_EMPTY(){ 
	$ErrorMsG = array();
	if (empty($_POST['First_Name']) || empty($_POST['Last_Name'])) { 
		$ErrorMsG[] = 'Please Enter a Correct Name in the NAME Fields!'; 
	} 
	if ((empty($_POST['Address1']) || empty($_POST['Address2'])) && empty($_POST['City'])) { 
		$ErrorMsG[] = 'Please Enter An Address'; 
	} 
	if (!isset($_POST['Email'])) { 
		$ErrorMsG[] = 'Please Enter an Email Address in the EMAIL ADDRESS Field!'; 
	} else { 
		$ErrorMsG[] = CHECK_EMAIL($_POST['Email']); 
	}
	echo '<pre>';
	print_r($ErrorMsG);
	echo '</pre>';
	return $ErrorMsG; 
} 

?>
Mac

VAlidate $_POST variables

Posted: Tue Jan 21, 2003 6:47 am
by kendall
hey,

thanks for the tips. question??

i saw that you $ErrorMsG = array();

Did i need to define $Error Msg as an array??

I thought $ErrorMsG[] automatically defines that?

Posted: Tue Jan 21, 2003 7:02 am
by twigletmac
I tend to do that so that there will be something returned by the function even if there are no error messages. That way I can just test the size of the array to determine if there's anything to show.

Mac