Reputation:
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?
Upvotes: 0
Views: 196
Reputation: 5796
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 ?
Upvotes: 0
Reputation: 2038
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;
}
}
Upvotes: 4