homepage Welcome to WebmasterWorld Guest from 54.205.254.108
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
php to add the value of ANY ticked box to a message
kapow




msg:1266647
 4:31 pm on Oct 16, 2002 (gmt 0)

I am creating an enquiry form with php.
A visitor may click any of about 100 check boxes. Usually I write some basic php - so if box 1 is ticked then 'blue widget' is added to a message that is sent to my client.

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?

 

lorax




msg:1266648
 5:24 pm on Oct 16, 2002 (gmt 0)

kapow,
The short answer is yes. You'll need to do some thinking and setup to make it work though. What you'll need to do is use a common name for the checkboxes (like widgetblue, widgetgreen, etc.) and then parse the form fields sent when the form is submitted looking for variables of name = widgetxxx. Stuff them into an array, a string or just use them.

RE: text input boxes, yes. Same methodology.

Make sense?

jatar_k




msg:1266649
 6:18 pm on Oct 16, 2002 (gmt 0)

You can also just run through them with a foreach statement. Read every element as key => value and then add the value of the element if selected.

andreasfriedrich




msg:1266650
 6:28 pm on Oct 16, 2002 (gmt 0)

Put all the values in an array and build your HTML code by looping over the array.

$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

lorax




msg:1266651
 8:09 pm on Oct 16, 2002 (gmt 0)

then add the value of the element if selected

I don't believe (meaning I'm not sure) you need to check if it's selected. A checkbox won't exist once the form is posted if it isn't checked so isset would be more appropriate. Or did I misunderstand you jatar_k?

jatar_k




msg:1266652
 9:41 pm on Oct 16, 2002 (gmt 0)

spot on lorax, but if you add text boxes then you would only want to throw the data in there if they are not blank or equal to a default value.

the foreach would cover every element in the form. I could have been a little more robust in my explanation.

kapow




msg:1266653
 3:38 pm on Oct 17, 2002 (gmt 0)

Thankyou everyone.
Hmmm, I should also have mentioned that I am a php beginner.

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?

jatar_k




msg:1266654
 5:55 pm on Oct 17, 2002 (gmt 0)

a very simple example would be

foreach ($HTTP_POST_VARS as $key => $value)
{
$emailmessage .= $value . "\r\n";
}

kapow




msg:1266655
 12:09 pm on Oct 18, 2002 (gmt 0)

Thankyou - I'll try it.

\n = new line
what is \r?

jatar_k




msg:1266656
 4:10 pm on Oct 18, 2002 (gmt 0)

carriage return

kapow




msg:1266657
 3:46 pm on Oct 21, 2002 (gmt 0)

If im using text boxes, is there a way to also print the name of the variable? e.g. if some of my variables are:
- $quantity_of_blue_widgets
- $quantity_of_red_widgets

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

dingman




msg:1266658
 3:59 pm on Oct 21, 2002 (gmt 0)


foreach ($_POST as $key => $value)
{
$message_text .= "$key = $value\n"; // \r\n for dos-style end-of-line
}

andreasfriedrich




msg:1266659
 4:19 pm on Oct 21, 2002 (gmt 0)

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

kapow




msg:1266660
 7:48 pm on Nov 6, 2002 (gmt 0)


I used:
foreach ($_POST as $key => $value) { $MESSAGE .= "$key = $value\n"; }

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?

jatar_k




msg:1266661
 7:54 pm on Nov 6, 2002 (gmt 0)

in your foreach just exclude the ones you don't want

if (empty($value)) continue;
if ($key == "submit") continue;

kapow




msg:1266662
 9:58 am on Nov 7, 2002 (gmt 0)

Thanks again Jatar_K

How would I exclude several items: submit, item3, item8?
Would it be something like this:

if ($key == "submit" ¦¦ "item3" ¦¦ "item8") continue;

andreasfriedrich




msg:1266663
 2:37 pm on Nov 7, 2002 (gmt 0)

if ($key == "submit" ¦¦ $key == "item3" ¦¦ $key == "item8") continue;

or

if (preg_match("'submit¦item3¦item8'", $key)) continue;

Andreas

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved