简体   繁体   English

JavaScript和PHP参数

[英]JavaScript and PHP parameters

I have a form that I validate it using the JavaScript. 我有一个表单,我使用JavaScript验证它。

Now when I submit the form I can't use the check boxes in that form. 现在,当我提交表单时,我无法使用该表单中的复选框。

This is a part of my JS that counts and validates the check boxes. 这是我的JS的一部分,它计算并验证复选框。

<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 that the form submits the data to is as follows (the portion of the check boxes): 表单提交数据的下一页如下(复选框的部分):

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

Echoing the $cases prints only one letter of the selected check box. 回显$cases仅打印所选复选框的一个字母。

I want to know what I am 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>

SOLUTION: 解:

I have managed to find a solution to the problem by googling ... 我通过谷歌搜索找到了解决问题的方法......

first the name of each check box have [] 首先,每个复选框的名称都有[]

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 the javascript I added this part: 然后,在javascript中我添加了这部分:

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

where addClients is the name of my form. 其中addClients是我的表单的名称。

then I tried the PHP that I have attached, and it listed the values as it should. 然后我尝试了我附加的PHP,它列出了应该的值。

In your input's name you need to add a [] to the end of the name. 在输入的名称中,您需要在名称的末尾添加[]。

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

The first thing that is wrong is that addClients is undefined. 首先是错误的是addClients是未定义的。 Perhaps you mean document.forms.id_of_form.addClients ? 也许你的意思是document.forms.id_of_form.addClients

The second thing that is wrong is that PHP's default form handling library, contrary to almost every other form data parsing library out there attaches special meaning to names ending in the characters [] . 第二件事是错误的是PHP的默认表单处理库,与几乎所有其他表单数据解析库相反,它们以字符[]结尾的名称附加特殊含义。 Since you appear to be looping over a set of elements, odds are that the name of the checkboxes is addClients[] and not addClients , so you would need: document.forms.id_of_form['addClients[]'] (using square bracket notation as [] has special meaning in JavaScript too). 由于您似乎是在一组元素上循环,因此复选框的名称是addClients[]而不是addClients ,因此您需要: document.forms.id_of_form['addClients[]'] (使用方括号表示法) as []在JavaScript中也有特殊含义)。

so: 所以:

$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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM