Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 10th, 2006, 4:41 PM   #11
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
The $_POST variable is just an array, so you can foreach to loop through it:

http://php.net/foreach
Polyphemus_ is offline   Reply With Quote
Old Feb 10th, 2006, 4:58 PM   #12
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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;?>&nbsp;<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
DaWei is offline   Reply With Quote
Old Feb 10th, 2006, 5:58 PM   #13
Lich
Professional Programmer
 
Lich's Avatar
 
Join Date: May 2005
Location: Detroit
Posts: 254
Rep Power: 4 Lich is on a distinguished road
Send a message via AIM to Lich Send a message via MSN to Lich
Quote:
Originally Posted by Polyphemus_
The $_POST variable is just an array, so you can foreach to loop through it:

http://php.net/foreach
or a while loop.

[PHP]while($_POST[])[/PHP]
__________________
--John Cruz
Web Developer
www.cruzweb.net
Lich is offline   Reply With Quote
Old Feb 10th, 2006, 6:09 PM   #14
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Feb 11th, 2006, 7:13 AM   #15
k4pil
Programmer
 
Join Date: Aug 2005
Location: Leeds - UK
Posts: 69
Rep Power: 4 k4pil is on a distinguished road
DaWei's code dsnt seem to work. It dsnt print out the checked boxes.
k4pil is offline   Reply With Quote
Old Feb 11th, 2006, 7:25 AM   #16
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Feb 11th, 2006, 7:34 AM   #17
k4pil
Programmer
 
Join Date: Aug 2005
Location: Leeds - UK
Posts: 69
Rep Power: 4 k4pil is on a distinguished road
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;?>&nbsp;<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/>"; 
?>
k4pil is offline   Reply With Quote
Old Feb 11th, 2006, 7:34 AM   #18
k4pil
Programmer
 
Join Date: Aug 2005
Location: Leeds - UK
Posts: 69
Rep Power: 4 k4pil is on a distinguished road
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;?>&nbsp;<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.
k4pil is offline   Reply With Quote
Old Feb 11th, 2006, 7:53 AM   #19
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
And it works.
Soooooo...your question is?

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
DaWei is offline   Reply With Quote
Old Feb 11th, 2006, 8:10 AM   #20
k4pil
Programmer
 
Join Date: Aug 2005
Location: Leeds - UK
Posts: 69
Rep Power: 4 k4pil is on a distinguished road
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;?>&nbsp;<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.
k4pil is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:38 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC