提交前的PHP复选框验证

时间:2016-06-17 00:51:50

标签: php forms checkbox form-submit submit-button

在完成此表单之前,我有最后一篇文章,但我认为模板中的函数是基于它使事情变得有点复杂。基本上我想在提交按钮执行命令之前需要一个“同意”复选框。

$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。下面是调用创建行/单元格/输入的函数。使用这种格式我不能简单地放入标签,我认为这是阻碍我的原因。

 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函数内部的“数组”参数吗?

2 个答案:

答案 0 :(得分:2)

替换

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

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

答案 1 :(得分:1)

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

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