I have an HTML form as:
<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" />
To execute the color form, I used a PHP as:
$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".)
I even changed my codes into:
$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?
I even changed my codes into:
$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?
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] . " ");
}
(untested. Code taken from here: http://www.html-form-guide.com/php-form/php-form-checkbox.html)
Try this:
PHP Code:
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] . " ");
}
}
}