Recover checkbox values with Validationengine

Asked

Viewed 151 times

1

I am trying to find the best way to send my fields via Ubmit to PHP to validate. I have a form with 4 checkboxes where at least one has to be valid.

I am using the Validation Engine, in PHP I work essentially with the Name of the countryside and with the Value.

I’m using an example like this:

<input class="validate[minCheckbox[2]]" type="checkbox" name="group1" id="maxcheck1" value="5"/>
<input class="validate[minCheckbox[2]]" type="checkbox" name="group1" id="maxcheck2" value="3"/>
<input class="validate[minCheckbox[2]]" type="checkbox" name="group1" id="maxcheck3" value="9"/>

Only that the Submit PHP takes the name of each field, so if the last check is selected, it mounts as if the "value" group1 were 1...

The Form Submit and Basics, just one input type="submit".....

I read the documentation and I’m finding it difficult in this case, someone can help me?

  • I think the question is missing: show the PHP Submit, put this tag and give a descriptive title

  • I edited @Brasofilo, and now? rs

  • 1

    You say the problem is in the Validation Engine, which is a jQuery plugin, speaks of PHP, but shows neither PHP nor jQuery... What is the difficulty?

  • @brasofilo . . . Validation Engine works like a class.... it works with the name of the input and with the validate[minCheckbox[2]] of the attribute class

  • @brasofilo PHP has already said... it recovers the group of checkBox as if the $_POST['group1'] were 1, it does not recover the ID of each, and yes the whole group..... I think it was your lack of attention, not poorly formulated question

1 answer

1


Well, after a lot of hard research and a lot of work, I was able to solve the problem, and I leave here the solution in case someone needs to use the validationEngine with checkBox....

To use the validationEngine in checkBox it is necessary to create the inputs as a array in HTML thus:

<input class="validate[required]" type="checkbox" name="group1[]" id="maxcheck1" value="a" onclick="checar(this.id)"/>
<input class="validate[required]" type="checkbox" name="group1[]" id="maxcheck2" value="b" onclick="checar(this.id)"/>
<input class="validate[required]" type="checkbox" name="group1[]" id="maxcheck3" value="c" onclick="checar(this.id)"/>

The array is defined in name="group1[]" [I didn’t know this, that you can create vectors in HTML this way, as knowledge] On the PHP side I use:

$_POST["maxcheck1"] = 0;
$_POST["maxcheck2"] = 0;
$_POST["maxcheck3"] = 0;

$checkBox = $_POST['group1'];
if ($checkBox) {
    $i = 0;
    foreach ( $checkBox as $value ) {
        switch ($value) {
            case "maxcheck1" :
                $_POST["maxcheck1"] = 1;
                break;
            case "maxcheck2" :
                $_POST["maxcheck2"] = 1;
                break;
            case "maxcheck3" :
                $_POST["maxcheck3"] = 1;
                break;
            default :break;
            $i++;
        }
    }
}

And finally, to recover these values in my form I use the functions:

function checkar() {    
    var idsObj = new Array("maxcheck1", "maxcheck2", "maxcheck3");    
    for (var i = 0; i < (idsObj.length); i++) {
        if (document.getElementById(idsObj[i]).value == 1) {
            //$(idsObj[i]).prop("checked", true);
            document.getElementById(idsObj[i]).value = idsObj[i];
            document.getElementById(idsObj[i]).checked = true;
        } else {
            //$(idsObj[i]).prop("checked", false);
            document.getElementById(idsObj[i]).value = 0;
            document.getElementById(idsObj[i]).checked = false;
        }
    }
}

function checar(idObj) {
    val = idObj;
    idObj = "#" + idObj;
    if ($(idObj).is(':checked')) {
        $(idObj).val(val);
    } else {
        $(idObj).val(0);
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.