Reputation: 1143
Currently I have a form that contains multiple checkboxes that you may check as many as you want.
The problem is that when I send them through my form to PHP, PHP only gets the last checkbox's value that was checked. How do I get all and store them as an array or some other way?
HTML:
<form action="php/submit.php" method="post" enctype="multipart/form-data">
<div class="spacing">
<a class="applicationLabel">Position(s) interested in:</a><br>
<input name="position" type="checkbox" value="Project Manager" /> Project Manager<br>
<input name="position" type="checkbox" value="Content Developer" /> Content Developer<br>
<input name="position" type="checkbox" value="Graphic Designer" /> Graphic Designer<br>
<input name="position" type="checkbox" value="Digital Media Creator & Storyteller" /> Digital Media Creator & Storyteller<br>
<input name="position" type="checkbox" value="Programmer" /> Programmer<br>
<input name="position" type="checkbox" value="Server Engineer/Technician" /> Server Engineer/Technician<br>
<input name="position" type="checkbox" value="User Experience Designer" /> User Experience Designer<br>
</div>
<input type="submit">
</form>
PHP:
<?php
$positions = $_POST['position'];
echo $positions;
?>
The output that I get is the value of the last checkbox that was pressed. The $positions variable only holds one value and not all. How do I make it hold all that were checked? Thanks in advance.
Upvotes: 6
Views: 9780
Reputation: 16502
Change name="position"
to name="position[]"
in all of your input
tags and then handle the $_POST['position']
variable as an array.
Use var_dump($_POST)
to see what data will be sent to PHP after you fix the name
attributes in your HTML.
Upvotes: 11