Page 1 of 1

dynamic naming and accessing of html objects ...

Posted: Sun Jan 19, 2003 11:25 am
by iamzek
I think this problem is similar to the "Form Checkboxes!!!" question but I'm rather new at this and didn't understand the example there ...

my problem is accessing the checkboxes on the form that is created ... I'm using a foreach to create a html "cell" and need to access the checkbox within the cell ...

i.e.

Code: Select all

// this is created from a db stored in a separate file
global $ItemsForSale;
$ItemsForSale = array( 
"stuff" => array ( "25.95" , 0 ),
"more stuff" => array ( 12.01, 0),
"etc" => array ( "TBA", 1) );
// then in the main file ( this is condenced )

Code: Select all

function makeTable( $words, $cost ) {
echo '<TR>
        <TD>' . $words . '</TD>   // this will unique
        <TD> $' . $cost . '</TD>
        <TD <INPUT TYPE="CHECKBOX" NAME="' . $words . '"></TD>  // the problem could be here
        </TR>';
&#125;
// I want to give the name of the checkbox the value of the words var

// this is how it uses maketable

Code: Select all

foreach ($ItemsForSale as $key => $value) &#123;
		makeTable($key, $value&#1111;0] );
	&#125;

// **** the problem is here (I think) *****
// accessing the value of the checkbox created with maketable

// what I'm tring to do here is loop through the origonal array to get the names of the checkboxes to see if it is checked ... if so, add only that data line ( of the origonal array to another called seleced items )

Code: Select all

$SelectedItems = array();

foreach ($ItemsForSale as $key=>$value) &#123;
  // use the item name to call the checkbox object by referance

 // **** this next line would need to understood as if ( stuff.value ) where 
 // stuff is the name of the checkbox from above

if ( $key.value ) &#123;    // if this Item was selected 

       $SelectedItems&#1111;$key] = $value&#1111;0];
	       // if $Item is a number ( not free ) tally
	       if ( is_int($value) )&#123; $Total += $Value; &#125;
	&#125; // else do nothing
&#125;


if there there is a better to do this please let me know ... I run into similar problems like this in other languages ... not sure if it's a bad way to do things or if it's just lack of skill.


thanks guys

Posted: Sun Jan 19, 2003 4:49 pm
by volka
you might replace your makeTable by

Code: Select all

<?php
// [...]
function makeTable( $words, $cost )
{ ?>
	<tr>
		<td><?php echo $words; ?></td>
		<td><?php echo $cost; ?></td>
		<td><input type="checkbox" name="SelectedItems[]" value="<?php echo $words; ?>" />
	</tr>
<?php        
}
// [...]
?>
with the example data this should produce (only the input-elements)

Code: Select all

&lt;input type="checkbox" name="SelectedItems&#1111;]" value="stuff" /&gt;
&lt;input type="checkbox" name="SelectedItems&#1111;]" value="more stuff" /&gt;
&lt;input type="checkbox" name="SelectedItems&#1111;]" value="etc" /&gt;
Let's assume the user checked all of them, you're not using register_globals and you form's method is POST. Then your script should receive the array

Code: Select all

$_POST['SelectedItems'] == array('stuff', 'more stuff', 'etc');
if only more stuff and etc have been selected it should be

Code: Select all

$_POST['SelectedItems'] == array('more stuff', 'etc');
you can iterate the values e.g. with

Code: Select all

foreach($_POST['SelectedItems'] as $selectedItem)