PHP 提交前的复选框验证

PHP Checkbox Validation before Submit

在完成此表单之前,我还有最后一篇文章,但我认为我所基于的模板中的功能使事情变得有点复杂。基本上我想在提交按钮执行命令之前需要一个 "agree" 复选框。

$tbl->addRow();

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

$tbl->addRow();

$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;
}

这是我的 php submit/checkbox。下面是被调用以创建 rows/cells/inputs 的函数。使用这种格式我不能简单地放入标签,我认为这就是阻碍我前进的原因。

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

如果有帮助,很高兴分享更多代码。谁能帮我正确格式化代码以适应 addInput 函数内的 "array" 参数?

替换

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

来自

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

但是,这很容易被绕过,我建议您在提交表单后添加验证脚本,如果还没有的话。

您需要将 required 属性添加到复选框。

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