ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  Autofilled PHP Forms
Subject:   Preselected radio buttons do not work
Date:   2006-04-28 16:32:30
From:   GavinAndresen
Response to: Preselected radio buttons do not work

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);