Capturing HTML form field names even when they are blank

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

    Capturing HTML form field names even when they are blank

    Hi, all. I'd like to do the following, preferably *without* resorting to
    JavaScript:

    I have a long, dynamically-generated form questionnaire. Not all of the
    form fields are dynamically generated, though.

    I'd like to capture the NAME of every HTML form field element on the
    server, even if that element is submitted blank. The trouble is, with,
    say, radio buttons or checkboxes for example, a *blank* element does not
    get submitted at all.

    Example:
    <form action="text.ph p">
    <input type="radio" name="firstbutt on" value="1">
    <input type="radio" name="firstbutt on" value="2">
    <input type="radio" name="firstbutt on" value="2">
    <input type="text" name="thetextbo x">
    </form>

    text.php
    <?php
    print_r($_REQUE ST);
    ?>

    If the form is submitted completely blank, text.php prints out:

    Array
    (
    [thetextbox] =>
    )

    ONLY if I click a value on one of the radio buttons do I get the field
    "firstbutto n", e.g.:

    Array
    (
    [thetextbox] =>
    [firstbutton] => 1
    )

    How can I get the names of all of the fields in the HTML form even if they
    are sent blank? I am considering using JavaScript onSubmit() to put fake
    values in for blank fields, but using JS is not desirable. I had been
    putting hidden fields in with the field name "field_name s[nameoffield]"
    for each form field but this does not scale well for any non-dynamic form
    fields.

    Any other ideas?

    --
    JDS | jeffrey@go.away .com
    | http://www.newtnotes.com
    DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

  • DH

    #2
    Re: Capturing HTML form field names even when they are blank

    JDS wrote:[color=blue]
    > Hi, all. I'd like to do the following, preferably *without* resorting to
    > JavaScript:
    >
    > I have a long, dynamically-generated form questionnaire. Not all of the
    > form fields are dynamically generated, though.
    >
    > I'd like to capture the NAME of every HTML form field element on the
    > server, even if that element is submitted blank. The trouble is, with,
    > say, radio buttons or checkboxes for example, a *blank* element does not
    > get submitted at all.
    >
    > Example:
    > <form action="text.ph p">
    > <input type="radio" name="firstbutt on" value="1">
    > <input type="radio" name="firstbutt on" value="2">
    > <input type="radio" name="firstbutt on" value="2">
    > <input type="text" name="thetextbo x">
    > </form>
    >
    > text.php
    > <?php
    > print_r($_REQUE ST);
    > ?>
    >
    > If the form is submitted completely blank, text.php prints out:
    >
    > Array
    > (
    > [thetextbox] =>
    > )
    >
    > ONLY if I click a value on one of the radio buttons do I get the field
    > "firstbutto n", e.g.:
    >
    > Array
    > (
    > [thetextbox] =>
    > [firstbutton] => 1
    > )
    >
    > How can I get the names of all of the fields in the HTML form even if they
    > are sent blank? I am considering using JavaScript onSubmit() to put fake
    > values in for blank fields, but using JS is not desirable. I had been
    > putting hidden fields in with the field name "field_name s[nameoffield]"
    > for each form field but this does not scale well for any non-dynamic form
    > fields.
    >
    > Any other ideas?
    >[/color]

    Perhaps this will help, or give you some ideas:

    $buffer = '';

    while(list($key , $val) = each($_POST)){
    if(!is_array($v al)){
    $val = stripslashes(tr im($val));
    $buffer .= $key.': '.$val."\n";
    }else{
    foreach(array_k eys($val) as $key){
    $val[$key] = stripslashes(tr im($val));
    $buffer .= $val[$key].': '.$val."\n";
    }
    }
    }

    echo "\n".nl2br($buf fer);

    Comment

    • Ken Robinson

      #3
      Re: Capturing HTML form field names even when they are blank


      JDS wrote:

      [snip]
      [color=blue]
      > Example:
      > <form action="text.ph p">
      > <input type="radio" name="firstbutt on" value="1">
      > <input type="radio" name="firstbutt on" value="2">
      > <input type="radio" name="firstbutt on" value="2">
      > <input type="text" name="thetextbo x">
      > </form>[/color]

      To do what you want, you have to initialize the fields using
      "type=hidde n". For example, using your example code:
      <form action="text.ph p" method="POST">
      <input type="hidden" name="firstbutt on" value="0">
      <input type="radio" name="firstbutt on" value="1">
      <input type="radio" name="firstbutt on" value="2">
      <input type="radio" name="firstbutt on" value="3">
      <input type="text" name="thetextbo x">
      </form>

      Now when someone submits the form without selecting anything, you will
      get a value of "0" in the variable $_POST['firstbutton'].

      You should be able to extend this technique to your real form.

      Ken

      Comment

      • Toby Inkster

        #4
        Re: Capturing HTML form field names even when they are blank

        JDS wrote:
        [color=blue]
        > I'd like to capture the NAME of every HTML form field element on the
        > server, even if that element is submitted blank. The trouble is, with,
        > say, radio buttons or checkboxes for example, a *blank* element does not
        > get submitted at all.[/color]

        Well sorry, you can't.
        [color=blue]
        > Example:
        > <form action="text.ph p">
        > <input type="radio" name="firstbutt on" value="1">
        > <input type="radio" name="firstbutt on" value="2">
        > <input type="radio" name="firstbutt on" value="2">
        > <input type="text" name="thetextbo x">
        > </form>[/color]

        <form action="text.ph p">
        <input type="radio" name="firstbutt on" value="1">
        <input type="radio" name="firstbutt on" value="2">
        <input type="radio" name="firstbutt on" value="2">
        <input type="text" name="thetextbo x">
        <input type="hidden" name="FIELDS" value="thetextb ox,firstbutton" >
        </form>


        --
        Toby A Inkster BSc (Hons) ARCS
        Contact Me ~ http://tobyinkster.co.uk/contact

        Comment

        • Andy Hassall

          #5
          Re: Capturing HTML form field names even when they are blank

          On Mon, 07 Feb 2005 14:50:51 -0500, JDS <jeffrey@go.awa y.com> wrote:
          [color=blue]
          >Hi, all. I'd like to do the following, preferably *without* resorting to
          >JavaScript:
          >
          >I have a long, dynamically-generated form questionnaire. Not all of the
          >form fields are dynamically generated, though.
          >
          >I'd like to capture the NAME of every HTML form field element on the
          >server, even if that element is submitted blank. The trouble is, with,
          >say, radio buttons or checkboxes for example, a *blank* element does not
          >get submitted at all.[/color]

          This behaviour is required by the HTML standard:



          [color=blue]
          >Example:
          ><form action="text.ph p">
          ><input type="radio" name="firstbutt on" value="1">
          ><input type="radio" name="firstbutt on" value="2">
          ><input type="radio" name="firstbutt on" value="2">
          ><input type="text" name="thetextbo x">
          ></form>
          >
          >If the form is submitted completely blank, text.php prints out:
          >
          >Array
          >(
          > [thetextbox] =>
          >)
          >
          >ONLY if I click a value on one of the radio buttons do I get the field
          >"firstbutton ", e.g.:
          >
          >Array
          >(
          > [thetextbox] =>
          > [firstbutton] => 1
          >)
          >
          >How can I get the names of all of the fields in the HTML form even if they
          >are sent blank? I am considering using JavaScript onSubmit() to put fake
          >values in for blank fields, but using JS is not desirable. I had been
          >putting hidden fields in with the field name "field_name s[nameoffield]"
          >for each form field but this does not scale well for any non-dynamic form
          >fields.[/color]

          Adding extra elements is AFAIK the only method, given that HTML requires that
          non-selected radio buttons are not successful.

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

          Comment

          • JDS

            #6
            Re: Capturing HTML form field names even when they are blank

            On Mon, 07 Feb 2005 20:50:37 +0000, Toby Inkster wrote:
            [color=blue]
            > Well sorry, you can't.[/color]

            Allright. Thanks all. I had been using hidden fields, anyways, which was
            fine for the dynamically-generated form fields because I can just have the
            hidden fields dynamically generated as well.

            However, manually putting in hiddens for the other fields does not scale
            well and is prone to errors. Oh well, I'll figure something out.

            Yum, coffee.

            --
            JDS | jeffrey@go.away .com
            | http://www.newtnotes.com
            DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

            Comment

            • Fred Oz

              #7
              Re: Capturing HTML form field names even when they are blank

              JDS wrote:[color=blue]
              > On Mon, 07 Feb 2005 20:50:37 +0000, Toby Inkster wrote:
              >
              >[color=green]
              >>Well sorry, you can't.[/color]
              >
              >
              > Allright. Thanks all. I had been using hidden fields, anyways, which was
              > fine for the dynamically-generated form fields because I can just have the
              > hidden fields dynamically generated as well.
              >
              > However, manually putting in hiddens for the other fields does not scale
              > well and is prone to errors. Oh well, I'll figure something out.
              >[/color]

              Read the HTML spec. For radio buttons, one option must *always*
              be selected. Therefore, the case you site should not happen,
              there should always be one selected and so one should always be
              submitted.

              <URL:http://www.w3.org/TR/html401/interact/forms.html#radi o>

              radio buttons

              Radio buttons are like checkboxes except that when several share
              the same control name , they are mutually exclusive: when one is
              switched "on", all others with the same name are switched "off".
              The INPUT element is used to create a radio button control.

              If no radio button in a set sharing the same control name is
              initially "on", user agent behavior for choosing which control
              is initially "on" is undefined. Note. Since existing
              implementations handle this case differently, the current
              specification differs from RFC 1866 ( [RFC1866] section
              8.1.2.4), which states:

              At all times, exactly one of the radio buttons in a set is
              checked. If none of the <INPUT> elements of a set of radio
              buttons specifies `CHECKED', then the user agent must check
              the first radio button of the set initially.

              Since user agent behavior differs, authors should ensure that in
              each set of radio buttons that one is initially "on".

              *Note the last sentence.*


              --
              Fred

              Comment

              • JDS

                #8
                Re: Capturing HTML form field names even when they are blank

                On Wed, 09 Feb 2005 01:49:27 +1000, Fred Oz wrote:
                [color=blue]
                > Since user agent behavior differs, authors should ensure that in each
                > set of radio buttons that one is initially "on".
                >
                > *Note the last sentence.*[/color]

                Great! Thanks for the info! This is very enlightening.

                --
                JDS | jeffrey@go.away .com
                | http://www.newtnotes.com
                DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

                Comment

                Working...