[英]PHP Checkbox Validation before Submit
在完成此表单之前,我有最后一篇文章,但我认为模板中的函数我的基础是让事情变得有点复杂。 基本上我想在提交按钮执行命令之前需要一个“同意”复选框。
$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函数内部的“数组”参数吗?
更换
$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) );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.