Article:
  Autofilled PHP Forms
Subject:   Preselected radio buttons do not work
Date:   2006-04-27 07:53:39
From:   jashnu
Thanks for the good code. However, I noticed that if a form has radio button with checked=checked the selection drops the first time you enter the form. Any solutions?
Main Topics Oldest First

Showing messages 1 through 1 of 1.

  • Preselected radio buttons do not work
    2006-04-28 16:32:30  GavinAndresen [View]

    Radio buttons are a pain, because "not checked" is represented by a LACK of a value in $_REQUEST.

    So fillInFormValues is doing the right thing-- if you pass it an empty array of values, it clears their checked="checked".

    To fix, you could either NOT pass your form through fillInFormValues() the very first time, OR you could pass in an array with the right stuff in it the very first time. Code would be something like:

    // Only call fillInFormValues if NOT first time:
    if (!empty($_POST)) {
    $formHTML = fillInFormValues($formHTML, $_POST);
    }

    OR:

    // First time: pass in correct checkbox states:
    if (empty($_POST)) {
    $values = array('checkbox1' => 1, 'checkbox2' => 1);
    }
    else {
    $values = $_POST;
    }
    $formHTML = fillInFormValues($html, $values);