Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending Multiple Checkbox Values in Form Post Request

Tags:

html

php

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.

like image 396
Nicolas Avatar asked Dec 26 '22 07:12

Nicolas


1 Answers

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.

like image 89
sjagr Avatar answered Dec 27 '22 22:12

sjagr