Jump to content

[SOLVED] loop within a function problems


thefortrees

Recommended Posts

The foreach loop is throwing the error "invalid argument supplied for foreach()" even though I defined the array right before it. Any ideas?

 

$required = array ('email', 'password1', 'password2', 'fname', 'lname', 'phone', 'address1', 'city', 'state', 'zip', 'nameoncert');

function checkData(){
	foreach($required as $index){		
		$value = $_POST[$index];
		if (empty($value) ){
			$msg = 'Please fill out all required fields.';				
			return $msg;
		}
	}

	if ($_POST['password1'] != $_POST['password2']){
		$msg = 'Password does not match.';
		return $msg;
	}

	if ( !checkEmailAddress($_POST['email']) ){
		$msg = 'Invalid e-mail address.';
		return $msg;
	}	
}

Link to comment
Share on other sites

<?php

function checkData($required){
	foreach($required as $index){		
		$value = $_POST[$index];
		if (empty($value) ){
			$msg = 'Please fill out all required fields.';				
			return $msg;
		}
	}

	if ($_POST['password1'] != $_POST['password2']){
		$msg = 'Password does not match.';
		return $msg;
	}

	if ( !checkEmailAddress($_POST['email']) ){
		$msg = 'Invalid e-mail address.';
		return $msg;
	}	
}

$required = array ('email', 'password1', 'password2', 'fname', 'lname', 'phone', 'address1', 'city', 'state', 'zip', 'nameoncert');
$result = checkdata($required);

?>

 

Alternatively you can do this:

 

<?php

$required = array ('email', 'password1', 'password2', 'fname', 'lname', 'phone', 'address1', 'city', 'state', 'zip', 'nameoncert');

function checkData(){

	global $required;

	foreach($required as $index){		
		$value = $_POST[$index];
		if (empty($value) ){
			$msg = 'Please fill out all required fields.';				
			return $msg;
		}
	}

	if ($_POST['password1'] != $_POST['password2']){
		$msg = 'Password does not match.';
		return $msg;
	}

	if ( !checkEmailAddress($_POST['email']) ){
		$msg = 'Invalid e-mail address.';
		return $msg;
	}	
}

?>

Link to comment
Share on other sites

You have scope problems.

 

Variables defined outside a function can not be seen inside a function.

 

You can do 1 of three things here:

  • Define the array inside the function

<?php
function checkData(){
               $required = array ('email', 'password1', 'password2', 'fname', 'lname', 'phone', 'address1', 'city', 'state', 'zip', 'nameoncert');
?>

[*] Define the array as global inside the function:

<?php
function checkData(){
               global $required;
?>

[*]Pass the array as an argument to the function:

<?php
$required = array ('email', 'password1', 'password2', 'fname', 'lname', 'phone', 'address1', 'city', 'state', 'zip', 'nameoncert');

function checkData($required){
?>


 

Ken

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.