Php $\u POST命令无法识别多选图像复选框值

Php $\u POST命令无法识别多选图像复选框值,php,html,forms,image,checkbox,Php,Html,Forms,Image,Checkbox,我的目标是:当用户单击“提交”时,我希望获得所有选定的值(也称为“吸引类型”)(当单击图片时,它应该像复选框一样工作)。CSS可以正常工作,但是当我到达php文件时,它会忽略我的选择并说“No Atrtype”,就像没有选择任何值一样 我对php(以及一般的编码)不熟悉,曾尝试对div重新排序,在php中使用“isset”,并更改变量名(以及更多),但都没有效果。我希望能在这里得到一些帮助 大多数类都与css属性有关,所以为了这个问题,请忽略css php: html: 海滩 古迹 主题公园

我的目标是:当用户单击“提交”时,我希望获得所有选定的值(也称为“吸引类型”)(当单击图片时,它应该像复选框一样工作)。CSS可以正常工作,但是当我到达php文件时,它会忽略我的选择并说“No Atrtype”,就像没有选择任何值一样

我对php(以及一般的编码)不熟悉,曾尝试对div重新排序,在php中使用“isset”,并更改变量名(以及更多),但都没有效果。我希望能在这里得到一些帮助

大多数类都与css属性有关,所以为了这个问题,请忽略css

php:

html:


海滩
古迹
主题公园
本性
购物中心
美食景点
老城
文化活动

这里有许多问题:


您的PHP将
$atrtype1
视为一个值,而不是一个值数组。尝试
print\r($\u POST['atrtype'])
查看实际数据,并相应调整代码。您不能
回显
数组

您有一个输入错误:

$atrtype1    = ...
if (empty($atrtype)) {
这是两个不同的变量

另外,停止使用
=
并通过使用
==
全类型强制转换限定符来改进您的编码风格

固定的:
//为什么还需要另一个变量?
如果($\服务器[“请求\方法”]=“发布”){
$\u POST['atrtype']=array_filter($\u POST['atrtype']);//删除空值
如果(计数($_POST['atrtype'])<1){
回声“无类型”;
} 
否则{
echo print_r($_POST['atrtype']);//输出数组中的所有值。
}
}

你的处理逻辑是一堆数组和字符串,你真的需要拿一张干净的纸,再次从一开始就判断你在做什么。如果不了解您的全部意图和逻辑流程,我就无法对此提供太多建议。

HTML基础知识:ID在HTML文档中必须是唯一的,您在此处的多个位置都违反了这一点。谢谢,我可能没有注意到。单击label元素将正确切换这些复选框的状态-如果不是事实,您将每个复选框包装到共享相同各自ID的div元素中。由于此错误,不会发生这种情况,因此您在提交表单时始终未选中任何复选框。此外,请检查您的结束。正确缩进代码有助于识别是否/从何处获得不匹配的标记。您似乎有4个太多,它们都在同一列中(靠近末尾),而在前面缺少一个。您的PHP将
$atrtype1
视为一个值,而不是一个值数组。尝试
print\r($\u POST['atrtype'])
查看实际数据,并相应调整代码。您不能
回显
数组。
<form action="get_triptypes.php" method="post" class="home_search_form" id="home_search_form">
    <div id="beaches" class="choice_item">
        <div class="checkbox-wrapper">
            <input name="atrtype[0]" type="checkbox" id="beaches" value="beaches" style="display:none;"/>
            <div class="choice_image">
                <label for="beaches"> <img src="images/destination_1.jpg" alt="" width="350" height="250" onclick="this.style.opacity = 0.4" ondblclick="this.style.opacity = 1"> </label>
            </div>
            <div class="choice_title">
                <h1 style="font-size:50px;">Beaches</h1>
            </div>
        </div>
    </div>

    <!-- Trip -->
    <div id="history" class="choice_item">
        <div class="checkbox-wrapper">
            <input name="atrtype[1]" type="checkbox" id="history" value="history" style="display:none;"/>
            <div class="choice_image">
                <label for="history"> <img src="images/history0.jpg" alt="" width="350" height="200" onclick="this.style.opacity = 0.4; " ondblclick="this.style.opacity = 1"> </label>
                <div class="choice_title">
                    <h1 style="font-size:40px;">Historic Sites</h1>
                </div>
            </div>
        </div>
    </div>

    <!-- Trip -->
    <div id="themeparks" class="choice_item">
        <div class="checkbox-wrapper">
            <input name="atrtype[2]" type="checkbox" id="themeparks" value="themeparks" style="display:none;"/>
            <div class="choice_image">
                <label for - "themeparks"> <img src="images/amusementpark.jpg" alt="" width="350" height="250" onclick="this.style.opacity = 0.4" ondblclick="this.style.opacity = 1"> </label>
                <div class="choice_title">
                    <h1 style="font-size:40px;">Theme Parks</h1>
                </div>
            </div>
        </div>
    </div>

    <!-- Trip -->
    <div id="nature" class="choice_item">
        <div class="checkbox-wrapper">
            <input name="atrtype[3]" type="checkbox" id="nature" value="nature" style="display:none;"/>
            <div class="choice_image">
                <label for="nature"><img src="images/nature.jpg" alt="" width="350" height="250" onclick="this.style.opacity = 0.4; Add(this);" ondblclick="this.style.opacity = 1;"></label>
                <div class="choice_title">
                    <h1 style="font-size:50px;">Nature</h1>
                </div>
            </div>
        </div>
    </div>
    <!-- Trip -->
    <div id="shopping" class="choice_item">
        <div class="checkbox-wrapper">
            <input name="atrtype[4]" type="checkbox" id="shopping" value="shopping" style="display:none;"/>
            <div class="choice_image">
                <label for="shopping"><img src="images/shopping.jpg" alt="" width="350" height="250" onclick="this.style.opacity = 0.4" ondblclick="this.style.opacity = 1"></label>
                <div class="choice_title">
                    <h1 style="font-size:40px;">Shopping Malls</h1>
                </div>
            </div>
        </div>
    </div>

    <!-- Trip -->
    <div id="food" class="choice_item">
        <div class="checkbox-wrapper">
            <input name="atrtype[5]" type="checkbox" id="food" value="food" style="display:none;"/>
            <div class="choice_image">
                <label for="food"><img src="images/food.jpg" alt="" width="350" height="250" onclick="this.style.opacity = 0.4" ondblclick="this.style.opacity = 1"></label>
                <div class="choice_title">
                    <h1 style="font-size:40px;">Foodie attractions</h1>
                </div>
            </div>
        </div>
    </div>

    <!-- Trip -->
    <div id="cities" class="choice_item">
        <div class="choice_image">
            <div class="checkbox-wrapper">
                <input name="atrtype[6]" type="checkbox" id="cities" value="cities" style="display:none;"/>
                <label for="cities"> <img src="images/oldtown.jpg" alt="" width="350" height="250" onclick="this.style.opacity = 0.4" ondblclick="this.style.opacity = 1"></label>
                <div class="choice_title">
                    <h1 style="font-size:40px;">Old towns</h1>
                </div>
            </div>
        </div>
    </div>

    <!-- Trip -->
    <div id="culture" class="choice_item">
        <div class="choice_image">
            <div class="checkbox-wrapper">
                <input name="atrtype[7]" type="checkbox" id="culture" value="culture" style="display:none;"/>
                <label for="culture"><img src="images/dance.jpg" alt="" width="350" height="250" onclick="this.style.opacity = 0.4;" ondblclick="this.style.opacity = 1"></label>
                <div class="choice_title">
                    <h1 style="font-size:30px;">Cultural Activities</h1>
                </div>
            </div>
        </div>
    </div>
    </div>
    </div>
    </div>
    </div>
    <div class="row load_more_row">
        <div class="col">
            <input type="submit" class="button home_search_button"/>
        </div>
</form>
$atrtype1    = ...
if (empty($atrtype)) {
// Why do you even need another variable?   
if ($_SERVER["REQUEST_METHOD"] === "POST") {
  
   $_POST['atrtype'] = array_filter($_POST['atrtype']); // remove empty values
   if (count($_POST['atrtype']) < 1) {
      echo "No Atrtype";
   } 
   else{
      echo print_r($_POST['atrtype']); //output all values in the array. 
   }

}