Welcome to WebmasterWorld Guest from 54.160.221.82

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

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

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 21, 2001
posts:1281
votes: 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?

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

Senior Member from US 

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Mar 31, 2002
posts:7575
votes: 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?

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

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 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.
6:28 pm on Oct 16, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 22, 2002
posts:1782
votes: 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

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

Senior Member from US 

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Mar 31, 2002
posts:7575
votes: 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?

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

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 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.

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 21, 2001
posts:1281
votes: 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?

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

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 0


a very simple example would be

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

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 21, 2001
posts:1281
votes: 0


Thankyou - I'll try it.

\n = new line
what is \r?

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

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 0


carriage return
3:46 pm on Oct 21, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 21, 2001
posts:1281
votes: 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

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Sept 12, 2002
posts:885
votes: 0



foreach ($_POST as $key => $value)
{
$message_text .= "$key = $value\n"; // \r\n for dos-style end-of-line
}
4:19 pm on Oct 21, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 22, 2002
posts:1782
votes: 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

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 21, 2001
posts:1281
votes: 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?

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

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 0


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

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

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 21, 2001
posts:1281
votes: 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;

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 22, 2002
posts:1782
votes: 0


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

or

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

Andreas