Problem filling an array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fernando Rodríguez

    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('valida ting_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'] );
    checkPhoneNumbe r( $_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_provi nce'] ); // 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($firstNa me)) {
    $errors['first_name'] = 'Please enter your first name.';
    }
    }

    function checkLastName( $lastname ) {
    if (empty($lastNam e)) {
    $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!


  • Darkstar 3D

    #2
    Re: Problem filling an array

    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.

    Comment

    • Pedro Graca

      #3
      Re: Problem filling an array

      Fernando Rodríguez wrote:[color=blue]
      > 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 ).[/color]
      <snip contents="code"/>[color=blue]
      > 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:-)[/color]

      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($firstNa me)) {
      $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($lastNam e)) {
      $errors['last_name'] = 'Please enter your last name.';
      }
      }

      --
      If you're posting through Google read <http://cfaj.freeshell. org/google>

      Comment

      • Jim Michaels

        #4
        Re: Problem filling an array

        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" <frr@easyjob.ne t> wrote in message
        news:a33bd8413a b78c7eb45fad217 ec@news.superne ws.com...[color=blue]
        >
        > 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('valida ting_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'] );
        > checkPhoneNumbe r( $_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_provi nce'] ); // 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($firstNa me)) {
        > $errors['first_name'] = 'Please enter your first name.';
        > }
        > }
        >
        > function checkLastName( $lastname ) {
        > if (empty($lastNam e)) {
        > $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!
        >
        >[/color]


        Comment

        Working...