sixfiveoh sixfiveoh - 10 months ago 35
PHP Question

PHP Checkbox Validation before Submit

I have one last piece before finishing this form, but I think the functions in the template I'm basing it off of are making things a little complicated. Basically I want to have an "agree" checkbox be required before the submit button executes its command.


$tbl->addCell( $frm->addInput('checkbox', 'checkbox', 'check'),
'submit', 'data', array('colspan'=>4) );


$tbl->addCell( $frm->addInput('submit', 'submit', 'Submit'),
'submit', 'data', array('colspan'=>4, 'onclick'=>'if(!this.form.checkbox.checked)return false};',) );

$frmStr = $frm->startForm('result.php', 'post', '', array('onsubmit'=>'return checkSubmit(this);') ) .
$tbl->display() . $frm->endForm();

return $frmStr;

Here is my php for the submit/checkbox. Below are the functions being called to create rows/cells/inputs. Using this format I can't simply put in tags and I think that's what's holding me back.

function addCell($data = '', $klass = '', $type = 'data', $attr_ar = array() ) {
$cell = array(
'data' => $data,
'klass' => $klass,
'type' => $type,
'atts' => $attr_ar

if ( empty($this->cur_section['rows']) ) {
try {
throw new Exception('You need to addRow before you can addCell');
} catch(Exception $ex) {
$msg = $ex->getMessage();
echo "<p>Error: $msg</p>";

// add to current section's current row's list of cells
$count = count( $this->cur_section['rows'] );
$curRow = &$this->cur_section['rows'][$count-1];
$curRow['cells'][] = &$cell;

function addInput($type, $name, $value, $attr_ar = array() ) {
$str = "<input type=\"$type\" name=\"$name\" value=\"$value\"";
if ($attr_ar) {
$str .= $this->addAttributes( $attr_ar );
$str .= $this->xhtml? ' />': '>';
return $str;

Happy to share more of the code if that will help. Can anyone help me with formatting the code properly to fit inside of the "array" argument inside of the addInput function?


You need to add the required attribute to the checkbox.

$tbl->addCell($frm->addInput('checkbox', 'checkbox', 'check', array('required' => 'required')),
            'submit', 'data', array('colspan'=>4) );