1

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. 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?

3
  • What framework are you using? Commented Jun 17, 2016 at 1:01
  • sorry, what do you mean by framework? just a php template i sourced with some js functions this is what I'm building off of if that's what you mean: dyn-web.com/php/order_form/example2.php Commented Jun 17, 2016 at 1:17
  • @sixfiveoh PHP can't do anything before the form is submitted. PHP runs on the server, not the client. Commented Jun 17, 2016 at 1:40

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

i used this for some of the radio switches in the form and it worked fine, but it doesn't seem to work for the checkbox? $tbl->addCell( $frm->addInput('checkbox', 'checkbox', 'check'), 'submit', 'data', array('colspan'=>4, 'required'=>true) ); doesn't seem to work. FWIW, i used 'required' => true for the radio switches, not 'required'=>'required'
The required attribute needs to be in the checkbox, not the cell.
@Barmar Yes, I agree. The parameter array('required' => 'required') must be placed in 3rd position of the addInput method. My bad.
@sixfiveoh In HTML5 you can required a form element using: <input type="text" name="firstname" required> or <input type="text" name="firstname" required="required">. Because you don't have put the method addAttributes in you first post, we can't see what is expected by the method and this is why I have suggested you to add array('required' => 'required'), in this case. If it works with true, this is better for the conformity with the W3C.
1

You need to add the required attribute to the checkbox.

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

2 Comments

a bit of an add-on question, is there no way using this function to add a text label to the left of the input? Right now I'm stuck putting any supporting text in a cell bordering the input box to add a description for the user.
It depends on the way the rest of your class works. Can you create a span containing the text and the checkbox, and use that as the contents of the cell?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.