1

I've been trying to figure this out for a while now and having issues as im new to PHP.

I need to have a checkbox that has to be checked before the form will be submitted to the database (registration form needing them to confirm the useragreement).

I am not sure where im going wrong and even after going through the other couple of posts on this matter im not having any luck with it.

below is the main bits of code that apply and anyone that could offer some guidance it would be much appreciated.

(the specific code for the check box is at the bottom of each portion as its the latest addition to the form)

From the validation

public function check($source, $items = array()) {
    foreach($items as $item => $rules) {
        foreach($rules as $rule => $rule_value) {

            $value = trim($source[$item]);

            if($rule === 'required' && $rule_value === true && empty($value)) {
                $this->addError("{$item} is required.");
            } else if (!empty($value)) {

                switch($rule) {
                    case 'min':
                        if(strlen($value) < $rule_value) {
                            $this->addError("{$item} must be a minimum of {$rule_value} characters.");
                        }
                    break;
                    case 'max':
                        if(strlen($value) > $rule_value) {
                            $this->addError("{$item} must be a maximum of {$rule_value} characters.");
                        }
                    break;
                    case 'matches':
                        if($value != $source[$rule_value]) {
                            $this->addError("{$rule_value} must match {$item}.");
                        }
                    break;
                    case 'unique':
                        $check = $this->_db->get('users', array($item, '=', $value));
                        if($check->count()) {
                            $this->addError("{$item} is already in use.");
                        }
                    break;
                    case 'accepted':
                        if(!isset($_POST['useragreement'])){
                            $this->addError("You must agree to our terms and conditions");
                        }
                    break;

From the actual form processing

if(Token::check(Input::get('token'))) {
    $validate = new Validate();
    $validation = $validate->check($_POST, array(
        'username' => array(
            'required' => true,
            'min' => 2,
            'max' => 20,
            'unique' => 'users'),
        'password' => array(
            'required' => true,
            'min' => 6),
        'password_again' => array(
            'required' => true,
            'matches' => 'password'),
        'firstname' => array(
            'required' => false,
            'min' => 2,
            'max' => 50),
        'lastname' => array(
            'required' => false,
            'min' => 2,
            'max' => 50),
        'dob' => array(
            'required' => false),
        'gender' => array(
            'required' => false),
        'nationality' => array(
            'required' => false),
        'email' => array(
            'required' => true,
            'min' => 6,
            'unique' => 'email'),
        'email_again' => array(
            'required' => true,
            'matches' => 'email'),
        'useragreement' => array(
           'accepted' => true)
    ));

HTML

<div class="regfieldcheck">
    <label for="useragreement"><span class="requiredfield">*</span> I agree to the User Agreement.</label>
    <input id="useragreement" type="checkbox" />
</div>
1
  • You've not given your checkbox a name. Therefore it will never appear in $_POST Commented Jul 9, 2014 at 13:00

2 Answers 2

1

Your HTML form only specifies the id="useragreement" which is just used for javascript/css purposes.

To get "useragreement" in the $_POST array (so your function can see it), you will need to specify the "name" too.

Try changing this...

<input id="useragreement" type="checkbox" />

to this...

<input id="useragreement" name="useragreement" type="checkbox" />
Sign up to request clarification or add additional context in comments.

2 Comments

thanks but it wasn't that, i just checked to be sure
Ok, no worries but remember to keep the name="useragreement" in there or PHP will never see it!
1

Go back to basics. Firstly i would assign a name to the checkbox...

 <input id="useragreement" type="checkbox" name="useragreement"/>

On the page where you are posted the form data too just insert

var_dump($_POST);

You can then view the data that has been post'd. You can try this with both the tick box ticked and unticked and view the different outcomes.

You can then you a simple IF statement. So...

if(isset($_POST['useragreement'])){
    if($_POST['useragreement']){
      //If True (ticked)
    }else{
      //If Fale (unticked)
    }
}

3 Comments

Probably want to wrap an isset round there if(isset($_POST['useragreement'])){ as it won't get submitted if it's not checked
its not showing up in the array? why would that be??
Are you sure it is wrapped in <form> tags and the save <form> tags that the submit button is in.

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.