我有一个HTML表单:
<form action="something.php" method="POST" enctype="multipart/form-data" id="color">
<fieldset>
<legend><h2><span style="color: #993300; text-shadow: #808080 2px 1px 2px; font-size: 25px;">Color Preferences:</span></h2></legend>
<p><table>
<tbody>
<tr>
<td width="350px;"><span style="color: #993300;">*</span> Colors you consider to use to your new Site:</td>
<td>
<br/>
<input type="checkbox" name="color[]" value="Black" /> Black<br/>
<input type="checkbox" name="color[]" value="Blue" /> Blue<br/>
<input type="checkbox" name="color[]" value="Brown" /> Brown<br/>
<input type="checkbox" name="color[]" value="Gray" /> Gray<br/>
<input type="checkbox" name="color[]" value="Green" /> Green<br/>
</td>
</tr>
</tbody>
</table></p>
</fieldset>
<input type="submit" name="submit" value="Submit" /> <input type="reset" name="reset" value="Reset" />
要执行颜色表单,我使用PHP作为:
$color = $_POST['color'];
if (!isset($color))
{
unset($_GET['do']);
$message = "Error = Colors are required. Please try again.";
break;
} else {
$N = count($color);
for($i=0; $i < $N; $i++){
echo($color[$i] . " ");
}
}
但是当我在输出区域上测试我的代码时,它只会给出一个“数组”。 你能帮我修一下这段代码吗? (我需要将所选颜色作为输出而不是“数组”。)
我甚至将代码更改为:
$color = $_POST['color'] && $_POST['color'] = array ('Black','Blue','Brown','Gray','Green','Orange','Pink','Purple','Red','Silver','Tan','White','Yellow','Dark','Light');
if (!isset($_POST['color']))
{
unset($_GET['do']);
echo "Error = Colors are required. Please try again.";
} else {
$n = count($_POST['color']);
echo("You selected $n color(s): ");
for($i=0; $i < $n; $i++)
{
echo($_POST['color'][$i] . " ");
}
}
但结果结果仍然只是“数组”而不是颜色名称。你能帮我解决这个问题吗?
我甚至将代码更改为:
$color = $_POST['color'] && $_POST['color'] = array ('Black','Blue','Brown','Gray','Green','Orange','Pink','Purple','Red','Silver','Tan','White','Yellow','Dark','Light');
if (!isset($_POST['color']))
{
unset($_GET['do']);
echo "Error = Colors are required. Please try again.";
} else {
$n = count($_POST['color']);
echo("You selected $n color(s): ");
for($i=0; $i < $n; $i++)
{
echo($_POST['color'][$i] . " ");
}
}
但结果结果仍然只是“数组”而不是颜色名称。你能帮我解决这个问题吗?
答案 0 :(得分:0)
使用color [],您将收到一个数组($ _POST ['color'])。您可以测试它是否为isset()或它是否为空()
如果它不为空,那么你可以迭代这些值,例如:
$n = count($_POST['color']);
echo("You selected $n color(s): ");
for($i=0; $i < $n; $i++)
{
echo($_POST['color'][$i] . " ");
}
(未经测试。代码取自此处:http://www.html-form-guide.com/php-form/php-form-checkbox.html)
答案 1 :(得分:0)
试试这个:
PHP代码:
if(isset($ _ POST ['submit'])){
if (!isset($_POST['color'])) {
unset($_GET['do']);
echo "Error = Colors are required. Please try again.";
} else {
$color = $_POST['color'];
$N = count($color);
for ($i = 0; $i < $N; $i++) {
echo($color[$i] . " ");
}
}
}