0

I am in a silly position where I can't figure out to get the values of the checked checkboxes.

 <form id="civilForm" method="POST" action="form.php" enctype="multipart/form-data">
<p>
 <label>
<input id="12D" name="programsRequested[]" type="checkbox" />
<span>12D</span>
</label>
</p>
<p>
<label>
<input id="xp" name="programsRequested[]" type="checkbox" />
<span>XPStorm</span>
</label>
</p>
<p>
<label>
<input id="autoTurn" name="programsRequested[]" type="checkbox" />
<span>AutoTurn</span>
</label>
</p>
<p>
<label>
<input id="hecras" name="programsRequested[]" type="checkbox" />
<span>HEC RAS</span>
</label>
</p>

Then I am using a php loop as there are a bunch more checkboxes coming.

It spins through fine, but only give me a list that says: on, on, on which correctly tells me how many I checked, however does not give me the value of the checked box.

$selectedPrograms  = 'None';
if(isset($_POST['programsRequested']) && is_array($_POST['programsRequested']) && count($_POST['programsRequested']) > 0){
    $selectedPrograms = implode(', ', $_POST['programsRequested']);
}

Is there something obvious I missing on how to get the values here?

6
  • var_dump($_POST); to see what is returned Commented Apr 23, 2018 at 6:19
  • array(4) { ["firstName"]=> string(4) "Drew" ["lastName"]=> string(3) "Vur" ["programsRequested"]=> array(2) { [0]=> string(2) "on" [1]=> string(2) "on" } ["submitCivil"]=> string(0) "" } Commented Apr 23, 2018 at 6:22
  • 1
    don't forget the value="whatever" attribute Commented Apr 23, 2018 at 6:24
  • just add attribute value="1" if you got value 1 in php it mean this checkbox is checked otherwise not. Also in loop you'll get only checked checkboxes Commented Apr 23, 2018 at 6:26
  • ahh the values! Not Ids my bad. I'm blind! Commented Apr 23, 2018 at 6:32

1 Answer 1

2

add every input element value

<input id="12D" name="programsRequested[]" type="checkbox" value="1" />

form not closed .

submit button also missing .

<input type="submit" name="submit" >
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, it is just a part of the form, I didn't grab all the code. It is excessively long. Also thankyou, I didn't see I forgot to put values. I put ids instead! This worked perfectly

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.