View Full Version : Function and Array Problems - Help Needed!

08-15-2007, 11:30 PM

I've been spending the last few hours trying to figure out why my simple include won't work. It is a bunch of functions, and it isn't improperly written, and no errors are given. I am trying to make a multidimensional array by using validation functions and outputting the appended array so that I can call the multidimensional array full of errors to display them. Here's the code:

include (sample function, not necessary to list them all):
PHP Code:

function checkAddress($address, &$errors) {
if (strlen($address) < 4)
$errors[] = array("address","Please enter your address!");

actual script:
PHP Code:

if (isset($_POST['submit'])) {
$firstname = $_POST['firstName'];
$lastname = $_POST['lastName'];
$address = $_POST['address'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zipcode = $_POST['zipCode'];
$country = $_POST['country'];
$cardnumber = $_POST['cardNumber'];
$cardtype = $_POST['cardType'];
$expmonth = $_POST['expMonth'];
$expyear = $_POST['expYear'];
$cvv2code = $_POST['cvv2code'];
$amount = $_POST['amount'];
$errors = array();
checkFirstName($firstname, $errors);
checkLastName($lastname, $errors);
checkAddress($address, $errors);
checkCity($city, $errors);
checkCountryAndState($state, $country, $errors);
checkZipCode($zipcode, $country, $errors);
checkCVV2($cvv2code, $errors);
checkAmount($amount, $errors);
checkCardType($cardtype, $errors);
checkCardNumber($cardnumber, $errors);

...and the screen just goes blank. No code, no nothing, and it has been doing it ever since I made the include. I have tried putting the code in the actual script, and I rewrote the whole thing, and nothing. I don't understand why it doesn't work. Any help is appreciated!

08-15-2007, 11:38 PM
Is it possible that the form isn't returning any errors? (eg you've filled out all the fields correctly)

I know that sounds stupid, but I've done dumber stuff before....

also try adding this line at the end:
print count($errors);

if that is a zero that proves nothing is in the array


08-15-2007, 11:43 PM
Its impossible because i submitted a completely blank form. I tried that, but nothing happens. The script almost "stops" after the include, and nothing happens. This shouldn't happen though.

08-15-2007, 11:50 PM
Can we see the 'include.php' code?


08-15-2007, 11:51 PM
function checkPassword($password, $password2, &$errors) {
if (strlen($password) < 6 or strlen($password) > 20)
$errors[] = array("password","Please enter a password that is 6-20 alphanumeric characters!");
else if ($password !== $password2)
$errors[] = array("password","The entered passwords do not match!");
else if (!ctype_alnum($password))
$errors[] = array("password","Your password can only contain numbers and letters.");

function checkEmail($email, &$errors) {
if (strlen($email) == 0)
$errors[] = array("email","Please enter your email address!");
else if (!preg_match('/^[a-z0-9.+_-]+@([a-z0-9-]+.)+[a-z]+$/i',$email))
$errors[] = array("email","Email address is not valid!");

function checkZipCode($zipcode, $country, &$errors) {
if ($country == "US") {
if (strlen($zipcode) !== 5 or !preg_match('/^[0-9][0-9][0-9][0-9][0-9]$/',$zipcode))
$errors[] = array("zipcode","Please enter your 5 digit US zip code!");
} else {
if (empty($zipcode))
$errors[] = array("zipcode","Please enter your postal code");

function checkFirstName($firstname, &$errors) {
if (!preg_match('/^[-A-Za-z]+$/',$firstname) or strlen($firstname) == 0)
$errors[] = array("firstname","Please enter your first name!");

function checkLastName($lastname, &$errors) {
if (!preg_match('/^[-A-Za-z]+$/',$lastname) or strlen($lastname) == 0)
$errors[] = array("lastname","Please enter your last name!");

function checkCountryAndState($state, $country, &$errors) {
$nonstate = array("AB","BC","MB","NB","NL","NT","NS","NU","PE","SK","ON","QC");
if ($state == "")
$errors[] = array("state","Please enter your state/province!");
else if ($country == "US" && in_array($state,$nonstate))
$errors[] = array("state","Please enter your correct state!");
else if ($country == "US" && $state == "Other")
$errors[] = array("state","Please enter your correct state!");
else if ($country == "CA" && !in_array($state,$nonstate))
$errors[] = array("state","Please enter your correct province!");

function checkAddress($address, &$errors) {
if (strlen($address) < 4)
$errors[] = array("address","Please enter your address!");

function checkCity($city, &$errors) {
if (!ctype_alpha($city) or strlen($city) == 0)
$errors[] = array("city","Please enter your city!");

function checkPhoneNumber($phone, &$errors) {
if (strlen($phone) <= 9)
$errors[] = array("phone","Please enter your phone number!");
else if (ctype_digit($phone) && strlen($phone) == 10)
$phone = substr($phone,0,3)."-".substr($phone,3,3)."-".substr($phone,6,4);
else if (strlen($phone) !== 12 or !preg_match('/^[0-9][0-9][0-9][-][0-9][0-9][0-9][-][0-9][0-9][0-9][0-9]$/',$phone))
$errors[] = array("phone","Please enter a valid 10-digit phone number!");

function checkRegion($other, $state, &$errors) {
if ($state == "Other" && strlen($other) == 0)
$errors[] = array("other","Please enter your region!");

function checkCVV2($cvv2code, &$errors) {
if (strlen($cvv2code) < 3 or !ctype_digit($cvv2code)
$errors[] = array("cvv2code","Please enter your 3 or 4 digit CVV2 code.");

function checkAmount($amount, &$errors) {
if (strlen($amount) == 0)
$errors[] = array("amount","There was a problem with the total of your order. Try starting over.");

function checkCardType($cardtype, &$errors) {
if ($cardtype == "")
$errors[] = array("cardtype","Please select your credit card type.");

function checkCardNumber($cardnumber, &$errors) {
if (!ctype_digit($cardnumber))
$errors[] = array("cardnumber","Please enter a valid credit card number.");

08-15-2007, 11:55 PM
Either $_POST['submit'] is not being set (you would need to post your form code to get help) or a fatal parse error is occurring.

Check your web server log for errors and/or turn on full PHP error reporting (both display_errors and the E_ALL reporting level) in php.ini or a .htaccess file. Turning on error reporting with code in your script does not display fatal parse errors because parse errors prevent your script from ever executing and any code to change the error reporting won't ever get executed.

Edit: The posted code produces the following error -

Parse error: syntax error, unexpected T_VARIABLE in ...your path\include.php on line 76

08-15-2007, 11:58 PM
Try returning $errors[] from each function, or making it global...

I don't know enough to know whether or not that would work, so maybe an expert can set me straight, but if I were having this problem, I would do that because I don't know enough to know whether it is correct or not.


08-16-2007, 12:02 AM
$_POST['submit'] is successful because it echoed "submit" (placed directly after that) when I was testing it. What is your line 76 consist of (yours may vary from mine). I will try returning each $errors[]; but I shouldn't have to make it global because its a reference in the function. Thanks

08-16-2007, 12:50 AM
Thanks, I got it now after turning display_errors on. I don't understand why it was off, it defaults to on, but thanks.