VAlidate $_POST variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

VAlidate $_POST variables

Post 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?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

VAlidate $_POST variables

Post 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?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply