Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP array_push() and array() issues

Status
Not open for further replies.

MJAZ

Programmer
Aug 1, 2006
73
US
Hello! Every time I run the script below in my browser I get this:

Code:
Warning: array_push() [function.array-push]: First argument should be an array in C:\Documents and Settings\Administrator\My Documents\Cyber Safe Deposit\website\signup\join.php on line 16

Warning: array_push() [function.array-push]: First argument should be an array in C:\Documents and Settings\Administrator\My Documents\Cyber Safe Deposit\website\signup\join.php on line 24

Warning: array_push() [function.array-push]: First argument should be an array in C:\Documents and Settings\Administrator\My Documents\Cyber Safe Deposit\website\signup\join.php on line 28

Warning: array_push() [function.array-push]: First argument should be an array in C:\Documents and Settings\Administrator\My Documents\Cyber Safe Deposit\website\signup\join.php on line 32

Here is my script:

Code:
$username=$_REQUEST["username"];
$password=md5($_REQUEST["pass"]);
$passverify=md5($_REQUEST["pass2"]);
$email=$_REQUEST["email"];
$month=$_REQUEST["month"];
$year=$_REQUEST["year"];
$terms=$_REQUEST["terms"];

$error=array();

function getValidationValue() {
	$iserror=false;
	
	if(!isset($username, $password, $email, $month, $year)) {
		array_push($error, "All fields are required. Please try again.");
		$iserror=true;
	}
	if($password!=$passverify) {
		array_push($error, "The two passwords you entered do not match. Please check and try again.");
		$iserror=true;
	}
	if($terms!="accepted") {
		array_push($error, "You must accept the terms and conditions to sign up.");
		$iserror=true;
	}
	if($month<1 or $month>12) {
		array_push($error, "The month supplied is not valid. Please do not tamper with our forms.");
		$iserror=true;
	}
	if($year<1900 or $year>1995) {
		array_push($error, "The year supplied is not valid. Please do not tamper with our forms.");
		$iserror=true;
	}
	if($iserror==true) {
		catchError();
	}
	elseif($iserror=false) {
		insertValues;
	}
}
function catchError() {
	echo "<p>There were problems with the values you supplied: <ul>";
		foreach($error as $value) {
			echo "<li>Error:" .$value. "</li>";
		}
		echo "</ul></p>";	
}
Any ideas what is causing the error? The array must be initialized to empty.
 
you are declaring $error outside the scope of the validation function.

to make it accessible try this

Code:
function getValidationValue(&$error){
or this
Code:
$error = getValidationValue();
//add conditions for testing error 
function getValidationValue(){
 $error=array();
 //remaining lines
 
//at end
 if (count($error) === 0){
 return true; //no errors
 } else {
 return $error;
 }
} //end function

or this

Code:
function getValidationValue(){
 global $error;
 // ...
} //end function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top