Forum Moderators: coopster
Is there php code to add the value of ANY ticked box to my message variable?
i.e. so that I can add more check boxes to the form without modifying the php.
Also, Can the same thing be done with text input boxes?
RE: text input boxes, yes. Same methodology.
Make sense?
$array = array(1<<0 => 'A.C.', 1<<1 => 'T.H.', 1<<2 => 'J.M.');
#
foreach($array as $key => $value) {
$s["$value$key"] = sprintf('<input type="checkbox" class="cbox"
name="form[%s][%s]" value="%s"
%s /> %s<br/>',
$name_of_checkbox_group, $key, $key,
($key & $data?
'checked="checked"' : ''), $value);
}
If you don´t care much for bit bashing or want to have more than 32 entries per group use an array like this:
$array = array('A.C.','T.H.','J.M.') To preselect certain elements you would then need to change $key & $data? to $key == $form[$name_of_checkbox_group][$key]? assuming you populate a $form array in the form that it will later be returned by PHP.
Hope this helps.
Andreas
Andreasfriedrich thanks for your example code
...(1<<0 => 'A.C.', 1<<1 => 'T.H.', 1<<2 => 'J.M.'); ...
is that php short-hand? Its lost on me.
Any chance of some example code lorax or jatar_k?
Is my question something that a beginner shouldn't attempt?
and the visitor enters quantity numbers into the text boxes. How do I get the resulting $message to include something like this:
quantity_of_blue_widgets 150
quantity_of_red_widgets 10
Andreasfriedrich thanks for your example code
...(1<<0 => 'A.C.', 1<<1 => 'T.H.', 1<<2 => 'J.M.'); ...
is that php short-hand? Its lost on me.
array(key => value, ...) is PHP´s way (stolen from Perl) to create an associative array (hash) [php.net].
$a << $b is the shift left bitwise operator [php.net]. It shift the bits of $a $b steps to the left. 1 << (0..31) has the effect of 2 to the power of (0..31), i.e. it gives you 1, 2, 4, 8, 16, 32 and so on.
This is not particularly useful for an array of people (as in my example - you might not have not the fact that those were people, now you know). It is very useful for properties of people, widgets or whatever. If you wanted to store whether those people were male/female, famous, musicians, actors, rich/poor, minors this approach cames in handy.
Build an array:
$props = array(
1<<0 => 'male',
1<<1 => 'famous',
1<<2 => 'musician',
1<<3 => 'actor',
1<<4 => 'rich',
1<<5 => 'minor',
)
This is the same as writing:
$props = array(
1 => 'male',
2 => 'famous',
4 => 'musician',
8 => 'actor',
16 => 'rich',
32 => 'minor',
)
The latter becomes harder to write once the numbers get bigger. Since I´m lazy I much rather count from 0 to 31 than calculate 2 to the power of 0 to 31.
See mysql - SET column type [webmasterworld.com] for an application of this example.
Andreas
It works, that has saved me a lot of code - THANKYOU!
Unused text fields appear in the message though e.g. Item7 =
is there a way to exclude unused text fields?
Also I didn't reolise the submit button counts as an items too. I get:
submit = SEND
Is there a way to exclude submit or to exclude specified items?