Checkbox validation issue

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

    Checkbox validation issue

    Hi,

    In my form, I have a checkbox that must be checked in order to process
    the form. By default, I want it to be checked when the page first
    appears. I can do that easily enough by hardcoding the "checked"
    property.

    My problem occurs when I try to validate it.

    If the visitor unchecks the checkbox, the form cannot be processed,
    and I must allow the user to correct the problem (along with any other
    fields that were not filled out correctly).

    I want to use CSS to change the labels of any missing or bad fields to
    red. That part seems to be coded correctly (see below).

    <tr>
    <td colspan="2" class="<? echo (!isset($checkT elephone_err) ?
    'form-labels-error ' : 'form-labels'); ?>"><input
    name="checkTele phone" type="checkbox" value="OK2Call" > Yes, I
    authorize you to contact me via telephone.</td>
    </tr>

    The part that I'm not sure about is how to default the checkbox to
    checked when the form first appears, and yet allow for
    unchecking/checking after the form is submitted a first time.

    Sorry. I know the explanation is a bit vague. I'm having trouble
    elaborating well. If you have any tips or suggestions, please let me
    know.

    - Eric
  • Agelmar

    #2
    Re: Checkbox validation issue

    Eric Linders wrote:
    <snip>[color=blue]
    > The part that I'm not sure about is how to default the checkbox to
    > checked when the form first appears, and yet allow for
    > unchecking/checking after the form is submitted a first time.[/color]

    Try:

    <?php
    if (empty($_POST) || (isset($_POST['checkTelephone '] &&
    $_POST['checkTelephone '] == "OK2Call")
    echo ' checked="checke d"';
    ?>

    When the form is first accessed, $_POST will be empty, and of course if
    $_POST is not empty and the value of checkTelephone is set, the box should
    still be checked...

    // Ian Fette
    // Proponent comp.lang.php


    Comment

    • CountScubula

      #3
      Re: Checkbox validation issue

      This doesn't sound like a php issue, unless you are sending the form
      to a php script then want to output the results back to the browser
      with the differnt colors.

      two ways,

      1: server side, php
      post the form to your script, validate it, if fails send it back to
      user, with new color

      2: client side, javascript
      define two styles
      ..normal {font-color:#000000; ,bla, bla,}
      ..bad {font-color:#FF0000; ,bla, bla,}
      with an onSubmit() do your validation then asign new style
      document.formna me.inputboxname .class = "bad";




      Mike Bradley
      http://gzen.myhq.info -- free online php tools

      Comment

      Working...