doumi5223 2016-08-23 12:44 采纳率: 100%
浏览 64
已采纳

复选框数组不发送值

I have been stuck on something simple and I just can't find what is the problem. Sometimes it's good to have a outside look on it.

So I have this 4 checkboxes.

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

In the PHP I do this check but it always end up in the else state.

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

When I fill out the form and submit it, I print the $_POST and get:

[only_job] => Array ( [0] => catB ) - which is is the correct checked checkbox. Respectively, catA when the first one is checked and etc.

I have been staring at it but just cant find the answer. Suggestions?

  • 写回答

2条回答 默认 最新

  • dongmi1872 2016-08-23 13:01
    关注

    Your conditions are not proper. Either you should turn all if and else block into if-elseif-else block or you should use switch case like this:

    $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条)

报告相同问题?

悬赏问题

  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?