簡體   English   中英

提交前的PHP復選框驗證

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM