【问题标题】:PHP Checkbox Validation before Submit提交前的 PHP 复选框验证
【发布时间】:2016-06-17 00:51:49
【问题描述】:

在完成此表单之前,我还有最后一张,但我认为我所依据的模板中的功能使事情变得有些复杂。基本上我希望在提交按钮执行其命令之前需要一个“同意”复选框。

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

【问题讨论】:

  • 你用的是什么框架?
  • 对不起,你说的框架是什么意思?只是我使用一些 js 函数获取的 php 模板,如果这就是您的意思,这就是我要构建的内容:dyn-web.com/php/order_form/example2.php
  • @sixfiveoh PHP 在表单提交之前不能做任何事情。 PHP 运行在服务器上,而不是客户端。

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


【解决方案1】:

替换

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

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

【讨论】:

  • 我将它用于表单中的一些单选开关,效果很好,但它似乎不适用于复选框? $tbl->addCell($frm->addInput('checkbox', 'checkbox', 'check'), 'submit', 'data', array('colspan'=>4, 'required'=>true) ) ;似乎不起作用。 FWIW,我使用 'required' => true 作为无线电开关,而不是 'required'=>'required'
  • required 属性需要在复选框中,而不是单元格中。
  • @Barmar 是的,我同意。参数array('required' =&gt; 'required') 必须放在addInput 方法的第三个位置。我的错。
  • @sixfiveoh 在 HTML5 中,您可以使用:&lt;input type="text" name="firstname" required&gt;&lt;input type="text" name="firstname" required="required"&gt; 来要求表单元素。因为您没有将方法 addAttributes 放在您的第一篇文章中,所以我们看不到该方法的预期效果,这就是为什么我建议您在这种情况下添加 array('required' =&gt; 'required')。如果它与 true 一起工作,这将更好地符合 W3C。
【解决方案2】:

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

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

【讨论】:

  • 一个附加问题,有没有办法使用这个函数在输入的左边添加一个文本标签?现在我被困在与输入框相邻的单元格中放置任何支持文本,以便为用户添加描述。
  • 这取决于其他班级的工作方式。你能创建一个包含文本和复选框的跨度,并将其用作单元格的内容吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-11
  • 2018-10-24
  • 1970-01-01
  • 1970-01-01
  • 2015-12-02
  • 2021-06-21
  • 1970-01-01
相关资源
最近更新 更多