a php code

Hannes Mayer h.mayer at inode.at
Mon Jul 26 17:18:21 UTC 2004


Hongwei Li wrote:
> Hi,
> 
> We have fc1 with php 4.3.6-1.3 and apache 2.  I have a problem with php
> codes and ask for help.
> 
> My question might be off topic of this list.  But, since I don't know a
> good mailing list of php groups, so I post it here.  If somebody can help
> me, or tell me a good place to ask, I'd greatly appreciate.
> 
> Here is my test code, called fm.php:
> 
> <?php
> if (!isset($pick)) {
>  echo "Select color(s) and submit the form below."; }
> else {
>  $j = count($pick);
>  for($i=0; $i<$j; $i++) {
>   echo "Pick <b>$pick[$i]</b> is Checked<br />"; }
>  }
> ?>
> 
> <form action="<?php echo $_SERVER['PHP_SELF']; ?>">
> <ol>
> <li>Red<input name="pick[]" type=checkbox value="Red" /></li>
> <li>Blue<input name="pick[]" type=checkbox value="Blue" /></li>
> <li>Yellow<input name="pick[]" type=checkbox value="Yellow" /></li>
> </ol>
> 
> <input type="submit" value="Submit!" />
> <input type="reset" value="Reset" />
> </form>

Try this one:

<?
$pick1 = $HTTP_GET_VARS{pick1};
$pick2 = $HTTP_GET_VARS{pick2};
$pick3 = $HTTP_GET_VARS{pick3};

if (!isset($pick1) && !isset($pick2) && !isset($pick3)) {
	echo "Select color(s) and submit the form below.";
} else {
	if ($pick1 != "") {
		echo $pick1." is checked<br>";
	}
	if ($pick2 != "") {
		echo $pick2." is checked<br>";
	}
	if ($pick3 != "") {
		echo $pick3." is checked<br>";
	}
}
?>

<form action="<? echo $_SERVER['PHP_SELF']; ?>">
<ol>
<li>Red<input name="pick1" type=checkbox value="Red" /></li>
<li>Blue<input name="pick2" type=checkbox value="Blue" /></li>
<li>Yellow<input name="pick3" type=checkbox value="Yellow" /></li>
</ol>

<input type="submit" value="Submit!" />
<input type="reset" value="Reset" />
</form>

If you use checkboxes, you must name them differently, because you
can select more than one.
If the user should only be able to select one color, use radiobuttons:

<?
$pick = $HTTP_GET_VARS{pick};

if (!isset($pick)) {
	echo "Select color(s) and submit the form below.";
} else {
	echo $pick." is checked<br>";
}
?>

<form action="<? echo $_SERVER['PHP_SELF']; ?>">
<ol>
<li>Red<input name="pick" type=radio value="Red" /></li>
<li>Blue<input name="pick" type=radio value="Blue" /></li>
<li>Yellow<input name="pick" type=radio value="Yellow" /></li>
</ol>

<input type="submit" value="Submit!" />
<input type="reset" value="Reset" />
</form>

Take a look at these examples.
If you have more PHP questions, you can email me directly too.

Cheers,
Hannes.





More information about the users mailing list