如何通过PHP从HTML表单输出复选框? [英] How to output checkboxes from an HTML form by PHP?
问题描述
我的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] . " ");
}
}
但是当我在输出区域上测试我的代码时,结果只给出一个数组". 您能帮我解决此代码吗? (我需要将选定的颜色作为输出而不是数组".)
But when I test my code over the output area it only giving an "Array" as a result. Can you please help me to fix this code? (I need to get the selected colors as an output instead of "Array".)
我什至将代码更改为:
$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] . " ");
}
}
,但结果仍然只是数组",而不是颜色名称.您能帮我解决这个问题吗?
but still the result outcome is only "Array" instead of colors name. can you please help me to resolve this issue?
我什至将代码更改为:
$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] . " ");
}
}
,但结果仍然只是数组",而不是颜色名称.您能帮我解决这个问题吗?
but still the result outcome is only "Array" instead of colors name. can you please help me to resolve this issue?
推荐答案
具有color [],您将收到一个数组($ _POST ['color']).您可以测试它是isset()还是为空()
with color[], you will receive an array ($_POST['color']). You can test if it is isset() or it is empty()
如果它不为空,那么您可以像这样遍历实例的值:
if it is NOT empty, then you can iterate over the values for instance like this:
$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 )
(untested. Code taken from here: http://www.html-form-guide.com/php-form/php-form-checkbox.html)
这篇关于如何通过PHP从HTML表单输出复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!