471,066 Members | 1,365 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,066 software developers and data experts.

Problem filling an array


Hi,

I'm writing code to validate fields in a form before saving to a db. All
the validating functions are in a separate script which is required. All
the validating functions add an error message to an array if the data doesn't
validate. I check if something went wrong with count($theArray).

Here's my code:

---------------------------------------------------------------------------
// An array to keep all the error messages
$errors = array();

// get the checking functions (which use $errors)
require('validating_fns.php');

// Process errors only if the submit button has been pressed.
if (!empty($_POST['Submit'])) {

// Each time there's an error, add an error message to the error array
// using the field name as the key.
//checkFirstName( $_POST['first_name']);
checkLastName( $_POST['last_name'] );
checkEmail( $_POST['email_address'] );
checkEmailAgain( $_POST['email_address'], $_POST['email_address_2'] );
checkPhoneNumber( $_POST['phone_number']);
checkAddress( $_POST['address_line_1']);
checkCity( $_POST['city'] );
checkZipCode( $_POST['zip_code'] );
@checkCountry( $_POST['country'] ); // It might not be set, if there was
nothing selected in the combobox
@checkState( $_POST['state_or_province'] ); // It might not be set, if
there was nothing selected in the combobox
checkPassword( $_POST['password'] );
etc...
---------------------------------------------------------------------------

validating_fns.php looks like this:

---------------------------------------------------------------------
<?php

// Functions that check all fileds

// first and last name
function checkFirstName( $firstName ) {
if (empty($firstName)) {
$errors['first_name'] = 'Please enter your first name.';
}
}

function checkLastName( $lastname ) {
if (empty($lastName)) {
$errors['last_name'] = 'Please enter your last name';
}
}

etc....
---------------------------------------------------------------------

Even though some fields do not validate, when I check the count($errors)
I get 0. Nothing was added! =:-O

What am I doing wrong? O:-)

Thanks!
Jan 19 '06 #1
3 1392
This is not how I would do it, but, you need global $errors; in every
check function.

I would have used 1 function that checked them all at once.

Jan 19 '06 #2
Fernando Rodríguez wrote:
I'm writing code to validate fields in a form before saving to a db. All
the validating functions are in a separate script which is required. All
the validating functions add an error message to an array if the data doesn't
validate. I check if something went wrong with count($theArray). <snip contents="code"/> Even though some fields do not validate, when I check the count($errors)
I get 0. Nothing was added! =:-O

What am I doing wrong? O:-)


The functions in the validating_fns.php do not access the $errors array
you defined in your main script; they access their own local variable by
the same name.

Either declare the $errors array global inside each function or pass it
as a parameter (I like this better)

/* declare $errors global */
function checkFirstName( $firstName ) {
global $errors;
if (empty($firstName)) {
$errors['first_name'] = 'Please enter your first name.';
}
}
/* pass $errors as a parameter (by reference, so that it can be changed) */
function checkLastName( $lastName, &$errors ) {
if (empty($lastName)) {
$errors['last_name'] = 'Please enter your last name.';
}
}

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Jan 19 '06 #3
are you accessing $errors as a global variable?
<?php function f(){$z=1;}$z=0; f(); print $z; ?>
0
<?php
function f(){global $z;$z=1;} $z=0;f();print $z; ?>
1

you can also access the variable as $GLOBAL['z'] from within f().

"Fernando Rodríguez" <fr*@easyjob.net> wrote in message
news:a3*************************@news.supernews.co m...

Hi,

I'm writing code to validate fields in a form before saving to a db. All
the validating functions are in a separate script which is required. All
the validating functions add an error message to an array if the data
doesn't validate. I check if something went wrong with count($theArray).

Here's my code:

---------------------------------------------------------------------------
// An array to keep all the error messages
$errors = array();

// get the checking functions (which use $errors)
require('validating_fns.php');

// Process errors only if the submit button has been pressed.
if (!empty($_POST['Submit'])) {

// Each time there's an error, add an error message to the error array
// using the field name as the key.
//checkFirstName( $_POST['first_name']);
checkLastName( $_POST['last_name'] );
checkEmail( $_POST['email_address'] );
checkEmailAgain( $_POST['email_address'], $_POST['email_address_2'] );
checkPhoneNumber( $_POST['phone_number']);
checkAddress( $_POST['address_line_1']);
checkCity( $_POST['city'] );
checkZipCode( $_POST['zip_code'] );
@checkCountry( $_POST['country'] ); // It might not be set, if there was
nothing selected in the combobox
@checkState( $_POST['state_or_province'] ); // It might not be set, if
there was nothing selected in the combobox
checkPassword( $_POST['password'] );
etc...
---------------------------------------------------------------------------

validating_fns.php looks like this:

---------------------------------------------------------------------
<?php

// Functions that check all fileds

// first and last name
function checkFirstName( $firstName ) {
if (empty($firstName)) {
$errors['first_name'] = 'Please enter your first name.';
}
}

function checkLastName( $lastname ) {
if (empty($lastName)) {
$errors['last_name'] = 'Please enter your last name';
}
}

etc....
---------------------------------------------------------------------

Even though some fields do not validate, when I check the count($errors) I
get 0. Nothing was added! =:-O

What am I doing wrong? O:-)

Thanks!

Feb 12 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by luigi | last post: by
24 posts views Thread by Bangalore | last post: by
1 post views Thread by rkmoray | last post: by
9 posts views Thread by Víctor | last post: by
2 posts views Thread by ajikoe | last post: by
3 posts views Thread by sk.rasheedfarhan | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.