Retrieve checkbox values with ValidationEngine

1

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

I'm using the Validation Engine, in PHP I work mainly with the Name field and 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 Submit of PHP takes the name of each field, in that case if the last check is selected, it mounts as if the "%" value of% was group1 ...

The Form Submit and Basic, just a 1 .....

I have read the documentation and am finding it difficult in this case, can anyone help me?

    
asked by anonymous 06.09.2014 / 20:50

1 answer

1

Well, after an arduous research, and a huge hassle, I was able to solve the problem, and I leave here the solution in case someone needs to use validationEngine with checkBox ....

To use validationEngine in checkBox it is necessary to create inputs as array in HTML like this:

<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 did not know this, you can create vectors in HTML in 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);
    }
}
    
08.09.2014 / 16:19