复选框数组不发送值

时间:2016-08-23 12:44:49

标签: php html

我一直坚持简单的事情,但我无法找到问题所在。有时在外面看一下是件好事。

所以我有4个复选框。

 <input id="inp_fjob"  <?=(isset($job) && $job=="catA") ? 'checked' : '' ?> onclick="disCheck('inp_fjob','inp_ojob','inp_ajob','');" required="" type="checkbox" name="only_job[]" data-parsley-maxcheck="1" value="catA" />
 <input id="inp_ojob" <?=(isset($job) && $job=="catB") ? 'checked' : '' ?> onclick="disCheck('inp_ojob','inp_fjob','inp_ajob','');" type="checkbox" name="only_job[]" value="catB">
 <input id="inp_ajob" <?=(isset($job) && $job=="catC") ? 'checked' : '' ?>  onclick="disCheck('inp_ajob','inp_ojob','inp_fjob','');" type="checkbox" name="only_job[]" value="catC" >
 <input id="inp_ljob" <?=(isset($job) && $job=="catD") ? 'checked' : '' ?>   type="checkbox" name="only_job[]" value="catD" />

在PHP中我做了这个检查,但它总是以 else 状态结束。

$chArray = isset($_POST['only_job']) ? $_POST['only_job'] : array();

foreach ($chArray as $cBox){
    if ($cBox == "catA"){
        $job = "This is my first job since last 6 April and I have not been receiving taxable Jobseeker's Allowance of taxable Incapacity Benefit or a state of occupational pension.";
        $nicategory = "A";
    }
    if ($cBox == "catB"){
        $job = "This is now my only job, but since last 6 April I have had another job, or have receved taxable Jobseeker's Allowance or Incapacity Benefit. I do not receive a state or occupational pension.";
        $nicategory = "B";
    }
    if ($cBox == "catC"){
        $job = "I have another job or receive a state or occupational pension.";
        $nicategory = "C";
    }
    if ($cBox == "catD"){
        $job = "If you left a course of Higher Education before last 6 April and received your first Student Loan instalment on or after 1 September 1998 and you have not fully repaid your student loan, tick this:";
        $nicategory = "D";
    } else {
        $job = "nope.";
        $nicategory = "nope.";
    }
}

当我填写表格并提交时,我打印$ _POST并获取:

  

[only_job] =&gt;数组([0] =&gt; catB) - 这是正确选中的复选框。当第一个被检查时,分别是catA

我一直在盯着它但却找不到答案。 建议?

2 个答案:

答案 0 :(得分:4)

你的条件不合适。您应该将所有ifelse块变为if-elseif-else块,或者您应该使用switch这样的情况:

$chArray = isset($_POST['only_job']) ? $_POST['only_job'] : array();

foreach ($chArray as $cBox){
    if ($cBox == "catA"){
        $job = "This is my first job since last 6 April and I have not been receiving taxable Jobseeker's Allowance of taxable Incapacity Benefit or a state of occupational pension.";
        $nicategory = "A";
    } elseif ($cBox == "catB"){
        $job = "This is now my only job, but since last 6 April I have had another job, or have receved taxable Jobseeker's Allowance or Incapacity Benefit. I do not receive a state or occupational pension.";
        $nicategory = "B";
    } elseif ($cBox == "catC"){
        $job = "I have another job or receive a state or occupational pension.";
        $nicategory = "C";
    } elseif ($cBox == "catD"){
        $job = "If you left a course of Higher Education before last 6 April and received your first Student Loan instalment on or after 1 September 1998 and you have not fully repaid your student loan, tick this:";
        $nicategory = "D";
    } else {
        $job = "nope.";
        $nicategory = "nope.";
    }
}

OR

foreach ($chArray as $cBox){
    switch($cBox){
        case "catA": 
            $job = "This is my first job since last 6 April and I have not been receiving taxable Jobseeker's Allowance of taxable Incapacity Benefit or a state of occupational pension.";
            $nicategory = "A";
            break;
        case "catB":
            $job = "This is now my only job, but since last 6 April I have had another job, or have receved taxable Jobseeker's Allowance or Incapacity Benefit. I do not receive a state or occupational pension.";
            $nicategory = "B";
            break;
        case "catC":
            $job = "I have another job or receive a state or occupational pension.";
            $nicategory = "C";
            break;
        case "catD":
            $job = "If you left a course of Higher Education before last 6 April and received your first Student Loan instalment on or after 1 September 1998 and you have not fully repaid your student loan, tick this:";
            $nicategory = "D";
            break;
        default:
            $job = "nope.";
            $nicategory = "nope.";
            break;
    }
}

答案 1 :(得分:0)

您应该在PHP中获取 var_dump()数据,这样您就可以确保在PHP中获取正确的数据。

它应该返回所选复选框的值数组。

如果它给出了预期的输出,则d.coder提供的答案应该有效。

当你尝试使用if-else时,如果选择了一个,你是否使用过任何javascript取消选中所有其他框?因为if-else意味着你应该只获得一个值。

另外,为什么你不使用单选按钮呢?