gpt4 book ai didi

javascript - 根据所选项目数量回显

转载 作者:行者123 更新时间:2023-11-28 03:24:15 24 4
gpt4 key购买 nike

<form action="exercise2.php" method="post"> 
Type your name: <input type="text" name="name2" ><br>

Toyota<input name="vehicle[]" type="checkbox" value="Toyota"><br>
Bmw<input name="vehicle[]" type="checkbox" value="Bmw"><br>
Audi<input name="vehicle[]" type="checkbox" value="Audi"><br>
Subaru<input name="vehicle[]" type="checkbox" value="Subaru"><br>

<input type="submit" name="submit">
</form>

** 所以事情是这样的;当我不选择任何车辆时,程序崩溃,而它应该进入情况 1。**

我想回显车辆,例如当选中两个或三个选项时。

<?php

$name = $_POST['name2'];
$vehicles = $_POST['vehicle'];
$cont=0;

foreach($vehicles as $i){
$cont++;
}

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;
}?>

最佳答案

您正在尝试迭代一个不存在的数组。当未选中时,表单不会在复选框上发送任何内容,因此您需要更改您的 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;
}

同时打开错误,它会告诉您它无法迭代

关于javascript - 根据所选项目数量回显,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58809551/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com