![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
|
|
|
|
|
|
#12 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Wow.
[php] <?php $text = array ("Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"); echo "Boxes checked: "; for ($i = 0; $i < 10; $i++) { if (!empty ($_GET ["CheckBox$i"])) echo $text [$i].", "; } echo "<br/>"; ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Checkbox Check</title> </head> <body> <form action="cbox.php" method="get" name="myForm"> <input name="ButtonWidget" type="submit" value="Submit"><br/> <?php for ($i = 0; $i < 10; $i++) { ?> Checkbox <?php echo $i;?> <input name="CheckBox<?php echo $i;?>" type="checkbox" value="<?php echo $text [$i];?>"><br/> <?php }?> </form> </body> </html> [/php]
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#13 | |
|
Professional Programmer
|
Quote:
[PHP]while($_POST[])[/PHP] |
|
|
|
|
|
|
#14 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
As a matter of fact, the upper for loop in my example was originally a foreach construct. I decided to represent the output with a numerical modifier, so I switched to a for to get the index number. Anyone learning PHP really needs to browse the manual and keep it handy when writing code. It's an excellent piece of documentation.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#15 |
|
Programmer
Join Date: Aug 2005
Location: Leeds - UK
Posts: 69
Rep Power: 4
![]() |
DaWei's code dsnt seem to work. It dsnt print out the checked boxes.
|
|
|
|
|
|
#16 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Funny. Here's a screen shot showing the checkboxes that were
checked on submission. Admittedly, I didn't recheck the boxes, but that's trivial. ![]()
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#17 |
|
Programmer
Join Date: Aug 2005
Location: Leeds - UK
Posts: 69
Rep Power: 4
![]() |
This prob sounds really stupid, but which part of the code goes where?
I have all this in one <?php
$text = array ("Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine");
echo "Boxes checked: ";
for ($i = 0; $i < 10; $i++)
{
if (!empty ($_GET ["CheckBox$i"])) echo $text [$i].", ";
}
echo "<br/>";
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Checkbox Check</title>
</head>
<body>
<form action="cbox.php" method="get" name="myForm">
<input name="ButtonWidget" type="submit" value="Submit"><br/>
<?php
for ($i = 0; $i < 10; $i++)
{
?>
Checkbox <?php echo $i;?> <input name="CheckBox<?php echo $i;?>" type="checkbox" value="<?php echo $text [$i];?>"><br/>
<?php }?>
</form>
</body>
</html>And this in cbox.php <?php
$text = array ("Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine");
echo "Boxes checked: ";
for ($i = 0; $i < 10; $i++)
{
if (!empty ($_GET ["CheckBox$i"])) echo $text [$i].", ";
}
echo "<br/>";
?> |
|
|
|
|
|
#18 |
|
Programmer
Join Date: Aug 2005
Location: Leeds - UK
Posts: 69
Rep Power: 4
![]() |
This prob sounds really stupid, but which part of the code goes where?
I have all this in one <?php
$text = array ("Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine");
echo "Boxes checked: ";
for ($i = 0; $i < 10; $i++)
{
if (!empty ($_GET ["CheckBox$i"])) echo $text [$i].", ";
}
echo "<br/>";
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Checkbox Check</title>
</head>
<body>
<form action="cbox.php" method="get" name="myForm">
<input name="ButtonWidget" type="submit" value="Submit"><br/>
<?php
for ($i = 0; $i < 10; $i++)
{
?>
Checkbox <?php echo $i;?> <input name="CheckBox<?php echo $i;?>" type="checkbox" value="<?php echo $text [$i];?>"><br/>
<?php }?>
</form>
</body>
</html>And this in cbox.php <?php
$text = array ("Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine");
echo "Boxes checked: ";
for ($i = 0; $i < 10; $i++)
{
if (!empty ($_GET ["CheckBox$i"])) echo $text [$i].", ";
}
echo "<br/>";
?>And it works. |
|
|
|
|
|
#19 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
Note that I am using just one file, cbox.php. When this file is served on initial contact, the get array is empty, this no boxes are checked. One could detect this state and suppress the "Boxes checked:" message on that initial contact. When the form is submitted the action is the same file. This is, thus, a cyclic request/response thangy. If you submit to a second file, that's fine. If cbox.php is your second file, note that it is NOT an HTML page. Your browser can render pure text files, also. You have nothing, of course, that returns you to your original page. Possibly you want ensuing activities on an additional page. That's your decision. You have to implement them, though. I have the feeling that you don't really have the concept of the client/server request/response paradigm down. If you intend to use it, you should research it a tad. It isn't a telephone conversation; it's more like a walkie-talkie (though not precisely). The client makes a request; the server responds. The connection is broken. It is a stateless transaction. Any knowledge of state (continuance of a transaction begun with a previous request/response) is purely artificial. One may use "sessions" or one may accomplish the same thing on one's own. "Sessions" are just artificial state preservation functions already in place for you, nothing magical about them. As an example, to preserve the state of the checkboxes, I would need to add to the code that dinks with the checkboxes now (the name modifiers, etc.) by generating a "checked" attribute in accordance with the contents of the "get" array.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#20 |
|
Programmer
Join Date: Aug 2005
Location: Leeds - UK
Posts: 69
Rep Power: 4
![]() |
The array is to be populated with data from a database
<?php
header("Cache-control: private"); //IE 6 Fix
error_reporting(E_ALL);
include 'connect.php';
$query = "SELECT * FROM product";
$result = mysql_query($query);
$num = mysql_numrows($result);
mysql_close();
$text = array("$num");
?>
<form action="cbox.php" method="get" name="myForm">
<input name="ButtonWidget" type="submit" value="Submit"><br/>
<?
$i=0;
while ($i < $num)
{
$productID=mysql_result($result,$i,"productID");
$text[$i] = "$productID";
?>
Checkbox <?php echo $i;?> <input name="CheckBox<?php echo $i;?>" type="checkbox" value="<?php echo $text [$i];?>"><br/>
</form>
</body>
</html>
<?
$i++;
}
for ($i = 0; $i < $num; $i++)
{
if (!empty ($_GET ["CheckBox$i"])) echo $text [$i].", ";
}
echo "<br/>";
?>But my code above is obv wrong. Any help? Last edited by k4pil; Feb 11th, 2006 at 8:34 AM. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|