How to validate basic html form?

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

    How to validate basic html form?

    I have a basic Feedback form - I want to prevent blank entries. The problem
    with the below code is that the form still Posts if the 'message' field is
    blank. The form will not post if the 'email_address' field is blank,
    however. Why doesn't it catch empty 'message' field?

    <form method="POST" action=
    "<?
    $message = $_POST['message'];
    $email_address = $_POST['email_address'];
    if ( empty($message) || empty($email_ad dress) )
    {
    }
    else
    {
    $email = $_REQUEST['email_address'];
    $message = $_REQUEST['message'];
    mail( me@mydomain.com, "My Feedback Form", $message, "From:
    $email_address" );
    }
    ?>"
    name="Feedback" style="font-family: Arial, Helvetica, sans-serif;
    font-size:12px">
    Email address:
    <input name="email_add ress" type="text" size="30"/><br />
    Message:<br />
    <textarea name="message" rows="12" cols="25">
    </textarea><br />
    <input type="submit" value="Send" style="font-family: Arial, Helvetica,
    sans-serif">
    </form>

    Thanks in advance.


  • steve

    #2
    Re: How to validate basic html form?

    "deko" wrote:[color=blue]
    > I have a basic Feedback form - I want to prevent blank entries.[/color]
    The[color=blue]
    > problem
    > with the below code is that the form still Posts if the
    > ’message’ field is
    > blank. The form will not post if the ’email_address’
    > field is blank,
    > however. Why doesn’t it catch empty ’message’
    > field?
    >
    > ...
    > Thanks in advance.[/color]

    deko, I would be very surprized if the form does not accept blank
    entry. An html form does not have any intelligence to validate blank
    entries. This is done via server side (like you are doing), or
    javascript.

    --
    http://www.dbForumz.com/ This article was posted by author's request
    Articles individually checked for conformance to usenet standards
    Topic URL: http://www.dbForumz.com/PHP-validate...ict134820.html
    Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=450215

    Comment

    • deko

      #3
      Re: How to validate basic html form?

      > deko, I would be very surprized if the form does not accept blank[color=blue]
      > entry. An html form does not have any intelligence to validate blank
      > entries. This is done via server side (like you are doing), or
      > javascript.[/color]

      Okay, I think I've got it figured out - I needed to trim the php variables.
      For some reason there are 3 tab characters in the textarea - is there a way
      to prevent the tab characters from appearing in the textarea? Why are they
      there in the first place?



      <form action=

      "<?

      $message = trim($_REQUEST['message']);

      $email_address = trim($_REQUEST['email_address']);

      if ( empty($message) || empty($email_ad dress) )

      {

      }

      else

      {

      mail( "me@mydomain.co m", "Feedback Form", $message, "From:
      $email_address" );

      }

      ?>" method="POST"

      name="Feedback" style="font-family: Arial, Helvetica, sans-serif;
      font-size:12px">

      Email address:

      <input name="email_add ress" type="text" size="30"/><br />

      Message:<br />

      <textarea name="message" rows="12" cols="25">

      </textarea><br />

      <input type="submit" value="Send" style="font-family: Arial,
      Helvetica, sans-serif">

      </form>


      Comment

      • steve

        #4
        Re: Re: How to validate basic html form?

        "deko" wrote:[color=blue][color=green]
        > > deko, I would be very surprized if the form does not accept blank
        > > entry. An html form does not have any intelligence to validate[/color]
        > blank[color=green]
        > > entries. This is done via server side (like you are doing), or
        > > javascript.[/color]
        >
        > Okay, I think I’ve got it figured out - I needed to trim the php
        > variables.
        > For some reason there are 3 tab characters in the textarea - is[/color]
        there[color=blue]
        > a way
        > to prevent the tab characters from appearing in the textarea? Why[/color]
        are[color=blue]
        > they
        > there in the first place?
        >
        >
        > ...
        > </form>[/color]

        Just remove the lines and spaces in the textrea so it reads
        <textarea.....> </textarea>

        --
        http://www.dbForumz.com/ This article was posted by author's request
        Articles individually checked for conformance to usenet standards
        Topic URL: http://www.dbForumz.com/PHP-validate...ict134820.html
        Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=450375

        Comment

        • Anders K. Madsen

          #5
          Re: How to validate basic html form?

          -----BEGIN PGP SIGNED MESSAGE-----
          Hash: SHA1

          On Fri, 30 Jul 2004 00:47:03 GMT
          "deko" <nospam@hotmail .com> wrote:
          [color=blue][color=green]
          > > deko, I would be very surprized if the form does not accept blank
          > > entry. An html form does not have any intelligence to validate
          > > blank entries. This is done via server side (like you are doing),
          > > or javascript.[/color]
          >
          > Okay, I think I've got it figured out - I needed to trim the php
          > variables. For some reason there are 3 tab characters in the textarea
          > - is there a way to prevent the tab characters from appearing in the
          > textarea? Why are they there in the first place?
          >[/color]

          Probably because you haven't read your HTML guide well enough...
          You've got tabs between <textarea> and </textarea>, therefore tabs (and
          a line-break) appears...

          Try with <textarea></textarea> instead.

          Madsen


          - --
          Anders K. Madsen --- http://lillesvin.linux.dk

          "There are 10 types of people in the world.
          Those who understand binary - and those who don't."

          -----BEGIN PGP SIGNATURE-----
          Version: GnuPG v1.2.4 (GNU/Linux)

          iD4DBQFBCesAlNH Je/JASHcRAkHNAJ47E Y6Fy21VgqcYStKi JlxVHLgTdgCYqJO 5
          M+Kbv/AfPy8FsVZ2IO3j0 g==
          =Kr7u
          -----END PGP SIGNATURE-----

          Comment

          • David Mackenzie

            #6
            Re: How to validate basic html form?

            On Thu, 29 Jul 2004 22:55:01 GMT, "deko" <nospam@hotmail .com> wrote:
            [color=blue]
            >I have a basic Feedback form - I want to prevent blank entries. The problem
            >with the below code is that the form still Posts if the 'message' field is
            >blank. The form will not post if the 'email_address' field is blank,
            >however. Why doesn't it catch empty 'message' field?
            >
            > <form method="POST" action=
            > "<?
            > $message = $_POST['message'];
            > $email_address = $_POST['email_address'];
            > if ( empty($message) || empty($email_ad dress) )
            > {
            > }
            > else
            > {
            > $email = $_REQUEST['email_address'];
            > $message = $_REQUEST['message'];
            > mail( me@mydomain.com, "My Feedback Form", $message, "From:
            >$email_address " );
            > }
            > ?>"[/color]

            I don't think you understand how server-side scripting works. The
            above code will generate an empty "action" attribute. The action
            attribute should be a program that accepts and processes the form
            data, e.g. action="mail.ph p".

            Why do you populate $message and $email_address from $_POST, then
            re-populate them later on from $_REQUEST ?

            You populate $email from $_REQUEST, but pass $email_address to the
            mail() funciton.

            You should also put quotes around your e-mail address in the mail()
            function.

            --
            David ( @priz.co.uk )

            Comment

            • deko

              #7
              Re: Re: How to validate basic html form?

              > Just remove the lines and spaces in the textrea so it reads[color=blue]
              > <textarea.....> </textarea>[/color]

              Got it. For some reason I thought form code would behave like PHP - I put
              in the line breaks for readability... rookie move...

              What about quotes?

              <textarea name="message" rows="12" cols="25"></textarea><br />
              --or--
              <textarea name="message" rows=12 cols=25></textarea><br />

              Both seem to work.


              Comment

              Working...