在表单Post请求中发送多个复选框值。

时间:2021-07-08 02:07:29

Currently I have a form that contains multiple radio buttons 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 radio buttons value that was checked. How do I get all and store them as an array or some other way?

问题是,当我通过表单将它们发送到PHP时,PHP只获得选中的最后一个单选按钮值。如何获取并将它们存储为数组或其他方式?

HTML:

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" value="Project Manager" type="checkbox" /> Project Manager<br>
    <input name="position" value="Content Developer" type="checkbox" /> Content Developer<br>
    <input name="position" value="Graphic Designer" type="checkbox" /> Graphic Designer<br>
    <input name="position" value="Digital Media Creator & Storyteller" type="checkbox" /> Digital Media Creator & Storyteller<br>
    <input name="position" value="Programmer" type="checkbox" /> Programmer<br>
    <input name="position" value="Server Engineer/Technician" type="checkbox" /> Server Engineer/Technician<br>
    <input name="position" value="User Experience Designer" type="checkbox" /> User Experience Designer<br>
</div>
<input type="submit">
</form>

PHP:

PHP:

<?php
$positions = $_POST['position'];
echo $positions;
?>

The output that I get is the value of the last radio button 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.

我得到的输出是按下的最后一个单选按钮的值。$position变量只包含一个值,而不是所有值。我如何让它保存所有被检查过的?提前谢谢。

1 个解决方案

#1


10  

Change name="position" to name="position[]" in all of your input tags and then handle the $_POST['position'] variable as an array.

在所有输入标签中,将name="position"改为name="position[]",然后以数组的形式处理$_POST['position']变量。

Use var_dump($_POST) to see what data will be sent to PHP after you fix the name attributes in your HTML.

使用var_dump($_POST)查看在修改HTML中的名称属性后将向PHP发送哪些数据。

#1


10  

Change name="position" to name="position[]" in all of your input tags and then handle the $_POST['position'] variable as an array.

在所有输入标签中,将name="position"改为name="position[]",然后以数组的形式处理$_POST['position']变量。

Use var_dump($_POST) to see what data will be sent to PHP after you fix the name attributes in your HTML.

使用var_dump($_POST)查看在修改HTML中的名称属性后将向PHP发送哪些数据。