JavaScript and PHP options

I have a form that I validate using JavaScript.

Now when I submit the form, I cannot use the checkboxes in this form.

This is the part of my JS that counts and checks the checkboxes.

<script type="text/javascript">
function validateForm()
{
    //My Declarations
    var cases = addClients.caseType.length;
    var total = 0;
    var fnReturn = true;
    //My Script goes here
    for (var idx = 0; idx < cases; idx++)
    {
        if (addClients.caseType[idx].checked == true)
        total += 1;
    }
    if (total == 0)
    {
        document.getElementById('caseErr').style.visibility =" visible";
        fnReturn = false;
    }
    else
       document.getElementById('caseErr').style.visibility = "hidden";

    return (fnReturn == true)?true:false;
}
</script>

The next page to which the form submits data is as follows (part of the flags):

<?php
$cases = $_POST['caseType'];
for ($i=0;$i<count($cases);$i++)
    echo "<li>$cases[$i] \n";

When pressed again, $casesonly one letter of the selected flag is displayed.

I want to know what I'm doing wrong here.

EDIT:

This is my form

<form id="addClients" method="post" action="confirmAdd.php" onsubmit="return validateForm();">
<table border="0">
    <tr align="center">
        <th align="center">Client Last Name:</th>
        <td colspan="3" align="center"><input type="text" name="clname" width="300%"></td>
        <td id="lnErr" style="visibility:hidden">Last Name Required!</td>
    </tr>
    <tr align="center">
        <th align="center">Client First Name:</th>
        <td colspan="3" align="center"><input type="text" name="cfname" width="300%"></td>
        <td id="fnErr" style="visibility:hidden">First Name Required!</td>
     </tr>
    <tr align="center">
        <th align="center">Client Telephone:</th>
        <td colspan="3" align="center"><input type="text" name="ctel" width="300%"></td>
        <td id="telErr" style="visibility:hidden">Telephone Required (without hyphins '-')!</td>
    </tr>
    <tr align="center">
        <th>Cases:</th>
        <td align="center">Rape<input type="checkbox" name="caseType[]" value="rape"></td>
        <td align="center">Drug Accusition<input type="checkbox" name="caseType[]" value="Drug Accusition"></td>
        <td align="center">Assult<input type="checkbox" name="caseType[]" value="Assult"></td>
        <td id="caseErr" style="visibility:hidden">At least one required!</td>
    </tr>
</table>
<input type="submit" value="Add Client">
</form>

DECISION:

I managed to find a solution to the problem using googling ...

first the name of each flag has []

Rape<input type="checkbox" name="caseType[]" value="rape">
Drug Accusition<input type="checkbox" name="caseType[]" value="Drug Accusition">
Assult<input type="checkbox" name="caseType[]" value="Assult">

then in javascript i added this part:

var t=0;
var c=addClients['caseType[]'];
for(var i=0;i<c.length;i++)
    c[i].checked?t++:null;

where addClientsis the name of my form.

then I tried PHP, which I embedded, and it listed the values ​​as it should.

+3
3

[] .

<input type='checkbox' name='caseType[]' />



<script type="text/javascript">
    function getCheckBoxes( formID, name ) {

        var form = document.getElementById(formID);
        var checkboxes = form.getElementsByTagName('input');
        var returnArray = new Array();

        for (var i=0; i<checkboxes.length; i++) {
            if(checkboxes[i].name == name) {
                returnArray.push(checkboxes[i]);
            }
        }

        return checkBoxes;
    }

    function validateForm()
    {


        //My Declarations
        var cases = getCheckBoxes('addClients', 'caseType[]');;
        var total = 0;
        var fnReturn = true;
        //My Script goes here
        for (var idx = 0; idx < cases.length; idx++)
        {
            if (cases[idx].checked == true)
            total += 1;
        }
        if (total == 0)
        {
            document.getElementById('caseErr').style.visibility =" visible";
            fnReturn = false;
        }
        else
           document.getElementById('caseErr').style.visibility = "hidden";

        return false;
    }
</script>
+3

, , , addClients undefined. , document.forms.id_of_form.addClients?

, , , PHP , , , []. , , , , addClients[], addClients, : document.forms.id_of_form['addClients[]'] ( [] JavaScript ).

+2

:

$cases = $_POST['caseType'];
//normalize to an array
if(!is_array($cases))$cases=array($cases);
//iterate over the cases.
foreach($cases as $key=>$case){
echo"<li>$case</li>\r\n";
}
0

Source: https://habr.com/ru/post/1774175/


All Articles