How to use what the function returns?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • varois83

    How to use what the function returns?

    Hi

    I am a newbie and struggling with my guestbook validation process.
    I have found the following function online to check the length of a
    form field.

    <?php
    function checkLength($st ring, $min, $max) {
    $length = strlen ($string);
    if (($length < $min) || ($length > $max)) {
    return FALSE;
    } else {
    return TRUE;
    }
    }
    ?>

    Now below is the area in my code where I have to fit what that function
    returns but I can't do it. The "Text" field is the one that has to be
    checked for a certain length (Like $min = 1, $max = 100).I would like
    to point to the error.html file if checkLength returns false.

    $Text = $_POST['Text'] ;
    $email = $_POST['email'] ;
    $name = $_POST['name'] ;

    if (empty($name) || empty($email)) {
    header( "Location: error.html" );

    }
    else
    {

    //The fields are entered in the database here if it all goes fine.
    Thanks a lot

    Patrick

  • Janwillem Borleffs

    #2
    Re: How to use what the function returns?

    varois83 wrote:[color=blue]
    > Now below is the area in my code where I have to fit what that
    > function returns but I can't do it. The "Text" field is the one that
    > has to be checked for a certain length (Like $min = 1, $max = 100).I
    > would like to point to the error.html file if checkLength returns
    > false.
    >
    > $Text = $_POST['Text'] ;
    > $email = $_POST['email'] ;
    > $name = $_POST['name'] ;
    >
    > if (empty($name) || empty($email)) {
    > header( "Location: error.html" );
    >[/color]

    if (!checkLength($ Text, 1, 100) || empty($name) || empty($email)) {
    ....
    }

    BTW, there's more to form validation than used in this code (per example, it
    will allow a single white space character to pass as a valid email address),
    but you will probably get to this when you have learned more about PHP.


    JW



    Comment

    • Geoff Berrow

      #3
      Re: How to use what the function returns?

      I noticed that Message-ID: <41f42256$0$262 19$18b6e80@news .euronet.nl>
      from Janwillem Borleffs contained the following:
      [color=blue]
      >if (!checkLength($ Text, 1, 100) || empty($name) || empty($email)) {[/color]


      Bit unfriendly sending people to an error page if they type too much.
      Still I suppose you have your reasons. Consider using some Javascript
      in addition to the PHP code to give the user instant feedback that they
      are over the limit
      --
      Geoff Berrow (put thecat out to email)
      It's only Usenet, no one dies.
      My opinions, not the committee's, mine.
      Simple RFDs http://www.ckdog.co.uk/rfdmaker/

      Comment

      • Andy Hassall

        #4
        Re: How to use what the function returns?

        On Sun, 23 Jan 2005 23:14:54 +0000, Geoff Berrow <bl@ckdog.co.uk > wrote:
        [color=blue]
        >I noticed that Message-ID: <41f42256$0$262 19$18b6e80@news .euronet.nl>
        >from Janwillem Borleffs contained the following:
        >[color=green]
        >>if (!checkLength($ Text, 1, 100) || empty($name) || empty($email)) {[/color]
        >
        >Bit unfriendly sending people to an error page if they type too much.
        >Still I suppose you have your reasons. Consider using some Javascript
        >in addition to the PHP code to give the user instant feedback that they
        >are over the limit[/color]

        Or the maxlength attribute if it's an <input>. Shame there isn't such an
        attribute on <textarea> though.

        --
        Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
        <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

        Comment

        • varois83

          #5
          Re: How to use what the function returns?

          Hi

          JB

          God I tried everything for 2 hours, it's almost not fair.A pair of
          parenthesis did me in.
          Look at what I had that came the closest.:

          if (empty($name) || empty($email) || (!checkLength($ Text, 1, 100))) {

          I also understand that there is a lot more to validation than that but
          practice will make perfect.

          Thanks so much for your help JB it's working now.

          Geoff

          Bit unfriendly sending people to an error page if they type too much.
          Still I suppose you have your reasons. Consider using some Javascript
          in addition to the PHP code to give the user instant feedback that they
          are over the limit.

          Just playing around with the code at this time Geoff, trying to use
          only PHP at this point. I am going to create a page that tells people
          that use more than 100 characters not to do so.
          Thanks a lot guys

          Patrick

          Comment

          • varois83

            #6
            Re: How to use what the function returns?

            JW

            I spoke too fast in my previous post and tested what you said about the
            code allowing a single space. It did!
            How do you fix that?

            Thanks again

            Patrick

            Comment

            • Stijn Verholen

              #7
              Re: How to use what the function returns?

              varois83 wrote:[color=blue]
              > JW
              >
              > I spoke too fast in my previous post and tested what you said about the
              > code allowing a single space. It did!
              > How do you fix that?
              >
              > Thanks again
              >
              > Patrick
              >[/color]

              regular expressions

              Comment

              • iMedia

                #8
                Re: How to use what the function returns?

                eregi()

                PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.

                It takes time to master.

                Comment

                • varois83

                  #9
                  Re: How to use what the function returns?

                  Thanks again guys will get on it right away.

                  Patrick

                  Comment

                  Working...