1 回答

TA贡献1852条经验 获得超7个赞
您正在尝试遍历不存在的数组。未选中时,表单不会在复选框上发送任何内容,因此您需要更改 php:
<?php
# If you are echoing this back, it's good to sanitize it a bit
$name = htmlspecialchars($_POST['name2']);
# Make the default be an empty array
$vehicles = (!empty($_POST['vehicle']))? $_POST['vehicle'] : [];
$cont=0;
# This won't fail because it can iterate an empty array
foreach($vehicles as $i){
$cont++;
}
# 0 should now work
switch($cont){
case 0:
echo "You should choose some favs";
break;
case 1:
echo "$name, you should look for more fav cars";
break;
case 4:
echo "$name, I think you have too many fav cars";
break;
}
同时打开错误,它会告诉你它不能迭代empty。
添加回答
举报