Welcome to WebmasterWorld Guest from 50.19.34.234

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

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

     

kapow

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

WebmasterWorld Senior Member 10+ Year Member



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

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

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



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

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

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



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

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

WebmasterWorld Senior Member 10+ Year Member



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

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

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



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

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

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



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

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

WebmasterWorld Senior Member 10+ Year Member



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

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

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



a very simple example would be

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

kapow

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

WebmasterWorld Senior Member 10+ Year Member



Thankyou - I'll try it.

\n = new line
what is \r?

jatar_k

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

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



carriage return

kapow

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

WebmasterWorld Senior Member 10+ Year Member



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

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

WebmasterWorld Senior Member 10+ Year Member




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

andreasfriedrich

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

WebmasterWorld Senior Member 10+ Year Member



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

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

WebmasterWorld Senior Member 10+ Year Member




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

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

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



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

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

kapow

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

WebmasterWorld Senior Member 10+ Year Member



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

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

WebmasterWorld Senior Member 10+ Year Member



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

or

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

Andreas

 

Featured Threads

Hot Threads This Week

Hot Threads This Month