0

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?

8
  • Is this always output "catB" or else? Commented Aug 23, 2016 at 12:53
  • @d.coder Hi. No - actually it outputs the right checked checkbox. Commented Aug 23, 2016 at 12:54
  • I am bit confused here. You are saying it outputs right checked checkbox. But in you post you have mentioned it always end up in the else state?? Commented Aug 23, 2016 at 12:56
  • hi, if you checked only one checkbox then you will get single value in array, if you want multiple checkbox of array then you have to checked on multible check box. Commented Aug 23, 2016 at 12:59
  • 1
    Are you afraid of using radios? Commented Aug 23, 2016 at 13:05

2 Answers 2

4

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;
    }
}
Sign up to request clarification or add additional context in comments.

10 Comments

this is the problem! you end up with the last else, becasue all of the conditions will be checked
Hey. Thanks for the input. Actually I had it with the IFELSE before, but I changed it when I was trying to fix it. When I do it like this I face another problem. The PHP does not INSERT into the database nothing at all, which leads me to the thought that the value for $job and $nicategory is not set at all.
If your code still not working or unable to insert into database then please provide your latest code in phpfiddle
I was avoiding the post the whole code because It is a bit long. What I find strange is that with the check that I did and value is set to "nope." - the else statement - the INSERT is done. But when I change it - nothing is inserted and no error is displayed !?
There must be some sort of error that you will have to check. Without looking into the code i can't tell.
|
0

You should do a var_dump() of the data you are getting in PHP so you can be sure you're getting the correct data in PHP.

It should return an array of values of the selected checkboxes.

If it's giving expected output, then the answer provided by d.coder should work.

And when you tried with if-else, had you used any javascript to uncheck all other boxes once one got selected ? Because if-else means you should be getting just one value.

Also, why aren't you using radio buttons for this ?

1 Comment

Hi. Thanks for the input. Well, firstly I made it with checkboxes and I was not expecting a problem that big. Now I will try with radiobuttons.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.