简体   繁体   English

提交前的PHP复选框验证

[英]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->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;
}

Here is my php for the submit/checkbox. 这是我的提交/复选框的PHP。 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? 任何人都可以帮我正确格式化代码以适应addInput函数内部的“数组”参数吗?

Replace 更换

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

by 通过

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

But, that is can easily bypassed, i suggest you to add a script of verifications after the form was submitted, if this is not already the case. 但是,这很容易被绕过,我建议你在提交表单后添加一个验证脚本,如果不是这样的话。

You need to add the required attribute to the checkbox. 您需要将required属性添加到复选框。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM