Making use of an array using php to report via email |
Making use of an array using php to report via email |
Adam S. |
Jul 14 2007, 02:58 PM
Post
#1
|
Group: Members Posts: 3 Joined: 14-July 07 Member No.: 3,338 |
The HTML that I have has two sections of code that creates check boxes next to several options.
Here is the code for one of them <strong>What are you looking for in an adventure group?</strong> <p><input type="checkbox" name="adventure" value="freinds" /> Freinds with common interests <input type="checkbox" name="adventure" value="planned" /> Planned activities <input type="checkbox" name="adventure" value="group" /> Group discounts <p><input type="checkbox" name="adventure" value="long_term" /> Long term relationships <input type="checkbox" name="adventure" value="dating" /> Dating <input type="checkbox" name="adventure" value="all" /> All of the above <p><p> Then I have a .php script that emails all the info obtained by the page. (See .php code at the end of this post) I have multiple questions so if you can answer one or more that would be great. 1.) The way the original coder wrote this, it is overwriting the previous check box with the next checkboxes value when multiple are selected. So if friends and dating are both selected only dating is reported in the email. Ive been able to figure out that I need to create an array by changing <p><input type="checkbox" name="adventure" value="freinds" /> Freinds with common interests to <p><input type="checkbox" name="adventure[]" value="freinds" /> Freinds with common interests Question is, is that all I need to do (on the HTML side) 2.) How do I change the .PHP to make use of this array? 3.) Right now this line in the .php $body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } Is setting up the body of the email. The client really wants it in excel somehow but i don't know how to do that, so as a workaround I want to change the body of the email so that it reports "field1,field2,field3,field4,field5,etc." this way the client can just copy the text of the email into a notepad doc and then save and close, then open as a CSV in excel. Question is, How do I edit the .php so that no extra txt is in the body of the email, just the text needed to create the .csv 4.) How is creating the array going to effect the format of the .csv. I mean if the array is reported as "array_item1,array_item2,array_item3,etc..." That is obviously going to mess up the CSV's format right, how would i fix that if that is the case That is all my questions. Sorry for the lengthy post, but I thought it better to put to much info then not enough. Here is the .php code HTML index.htm page is attached <?php $to = "meonmyblackberry@gmail.com" ; $from = $_REQUEST['Email_Address'] ; $name = $_REQUEST['Name'] ; $headers = "From: $from"; $subject = "Web Contact Data"; $fields = array(); $fields{"marital"} = "Marital Status= "; $fields{"adventure"} = "What are you looking for in an adventure group?= "; $fields{"activities"} = "what activities?= "; $fields{"how_did_you_hear_of_us"} = "How did you hear of us?= "; $fields{"Name"} = "Name= "; $fields{"month"} = "month= "; $fields{"day"} = "day= "; $fields{"Birth_Year"} = "Birth Year= "; $fields{"Street_Address"} = "Street address= "; $fields{"City"} = "City= "; $fields{"State"} = "State= "; $fields{"ZIP_Code"} = "zip= "; $fields{"Email_Address"} = "Email= "; $fields{"Phone_Area_Code"} = "phone area code= "; $fields{"Phone_Number"} = "phone number= "; $fields{"phone_number_type"} = "phone type= "; $fields{"alt_Area_Code"} = "alt area code= "; $fields{"alt_Number"} = "alt phone number= "; $fields{"alt_number_type"} = "alt phone type= "; $fields{"terms"} = "agreed to terms of service= "; $body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } $headers2 = "From: meonmyblackberry@gmail.com"; $subject2 = "Thank you for your enquiry"; $autoreply = "Thank you for your enquiry. You are now registered you for a free E-mail Newsletter. We have forwarded your info to the Adventure Group in your area. They will contact you shortly to answer any questions you may have and to tell you about their Adventure Group. Please be patient as they may be out whitewater rafting, wine tasting or at one of dozens of events they plan each month "; if($from == '') {print "You have not entered an email, please go back and try again";} else { if($name == '') {print "You have not entered a name, please go back and try again";} else { $send = mail($to, $subject, $body, $headers); if($send) {header( "Location: http://server.com/index.htm" );} else {header( "Location: http://server.com/index.htm" );} } } ?> Attached File(s) index.htm ( 14.34k ) Number of downloads: 768 |
Adam S. |
Jul 17 2007, 10:48 PM
Post
#2
|
Group: Members Posts: 3 Joined: 14-July 07 Member No.: 3,338 |
80 some odd views and not one person can/wants to answer any of my questions.
Did I post in the wrong forum? |
Darin McGrew |
Jul 18 2007, 01:21 AM
Post
#3
|
WDG Member Group: Root Admin Posts: 8,365 Joined: 4-August 06 From: Mountain View, CA Member No.: 3 |
I didn't answer the first time, because I haven't used PHP before. But here's an answer based on other server-side programming.
1.) The way the original coder wrote this, it is overwriting the previous check box with the next checkboxes value when multiple are selected. The change in the name isn't needed unless PHP needs it. With CGI.pm, a Perl CGI program can easily access all the <input type="checkbox" name="adventure" value=...> elements that have been selected. The important thing from the HTML side is that they all have the same name.So if friends and dating are both selected only dating is reported in the email. Ive been able to figure out that I need to create an array by changing <p><input type="checkbox" name="adventure" value="freinds" /> Freinds with common interests to <p><input type="checkbox" name="adventure[]" value="freinds" /> Freinds with common interests Question is, is that all I need to do (on the HTML side) 4.) How is creating the array going to effect the format of the .csv. That depends on the format expected by whatever is reading your CSV, but you should be able to do something likeI mean if the array is reported as "array_item1,array_item2,array_item3,etc..." That is obviously going to mess up the CSV's format right, how would i fix that if that is the case CODE John Doe,"friends,planned,group",123 Main Street,Anytown If you can't use quotes like that, then you'll need some other separator, for exampleCODE John Doe,friends;planned;group,123 Main Street,Anytown |
Adam S. |
Jul 18 2007, 01:30 PM
Post
#4
|
Group: Members Posts: 3 Joined: 14-July 07 Member No.: 3,338 |
The change in the name isn't needed unless PHP needs it. With CGI.pm, a Perl CGI program can easily access all the <input type="checkbox" name="adventure" value=...> elements that have been selected. The important thing from the HTML side is that they all have the same name. If that is true then the current code should work, if you look at the attached idex.htm or the code snipit that i pasted in the post. The name on all the checkboxes is Adventure. but when another checkbox named adventure is checked it is overwriting the previous. So when friends and dating are checked its only showing dating since it is lower in the code order. When i change (name="adventure") to (name="adventure[]") the PHP just reports Array instead of the actual values of the array. Im not sure what that means. Any help would be great, ive been searching (this site and Google) but don't know enough about the subject to do an efficient search. Thanks Adam |
Brian Chandler |
Jul 19 2007, 04:24 AM
Post
#5
|
Jocular coder Group: Members Posts: 2,460 Joined: 31-August 06 Member No.: 43 |
QUOTE QUOTE(Darin McGrew @ Jul 17 2007, 11:21 PM) * The change in the name isn't needed unless PHP needs it. With CGI.pm, a Perl CGI program can easily access all the <input type="checkbox" name="adventure" value=...> elements that have been selected. The important thing from the HTML side is that they all have the same name. If that is true then the current code should work, if you look at the attached idex.htm or the code snipit that i pasted in the post. You have (>) 3 checkboxes with the same, like this: <p><input type="checkbox" name="adventure" value="long_term" /> Long term relationships <input type="checkbox" name="adventure" value="dating" /> Dating <input type="checkbox" name="adventure" value="all" /> All of the above <p><p> The PHP $_GET[] array only passes one of them (the last, I think). Two ways round this: (1) Change the html (simplest, by far) A checkbox gives exactly one bit of information (Yes or No), so there is no need for a "meaningful" _value_. Simply change these to: <p><input type="checkbox" name="advlong_term" value="x" /> Long term relationships <input type="checkbox" name="advdating" value="x" /> Dating <input type="checkbox" name="advall" value="x" /> All of the above <p><p> ... or whatever. (2) Look in the manual, and find how to access the query stuff directly (I've forgotten). Perhaps you have to parse the query string, which is no big deal, but not as simple as (1). |
D.Stoner |
Dec 4 2007, 10:58 AM
Post
#6
|
Group: Members Posts: 1 Joined: 4-December 07 Member No.: 4,472 |
Not that almost 2 years later will this post help you, but it might help others who are having this same problem using a similar code for php emails.
You were right to use this set up for your checkboxes: <input name="blah[]" type="checkbox" value="value1"> To make the coding grab these values you can do the following: Yours looks like: $body = "We have received the following information:\n\n"; foreach($fields as $a => $b) {$body .=sprintf("%s: %s",$b,$_REQUEST[$a]);} All that I did was edit this portion of the code like so: $whatyouwantittosay = "The following is what you have choosen "; $body = "We have received the following information:\n\n"; foreach($fields as $a => $b) {$body .=sprintf("%s: %s\n$divide\n",$b,$_REQUEST[$a]);} if (is_array ($_POST['blah'])){ foreach($_POST['blah'] as $c) {$body .=sprintf("$whatyouwantittosay: %s",$c);}} I took this one step further since the email output is kind of hard to read and entered in a divider so that items are separted and some spaces as well: $whatyouwantittosay = "The following is what you have choosen "; $divide = "`````````````````````````````````````````````````````````````"; $body = "We have received the following information:\n\n"; foreach($fields as $a => $b) {$body .=sprintf("%s: %s\n$divide\n",$b,$_REQUEST[$a]);} if (is_array ($_POST['blah'])){ foreach($_POST['blah'] as $c) {$body .=sprintf("$whatyouwantittosay: %s\n$divide\n",$c);}} Here is the output: The following is what you have choosen : value1 ````````````````````````````````````````````````````````````` The following is what you have choosen : value2 ````````````````````````````````````````````````````````````` The following is what you have choosen : value3 ````````````````````````````````````````````````````````````` No, I have yet to figure a way to make it so that these would display like so: The following is what you have choosen : value1, value2, value3 ````````````````````````````````````````````````````````````` Hope that this helps someone:) |
Matto |
Nov 1 2008, 09:20 AM
Post
#7
|
Group: Members Posts: 1 Joined: 1-November 08 Member No.: 7,026 |
Well there you go, it's November 2008 and this post helped me greatly! Thanks!
I was doing a similar thing, building an application that generated checkboxes over a series of pages that would form parts of a catering menu. Clients would pick which dishes they wanted to order by checking a checkbox, and then this would be emailed to both parties. I wanted the checkboxes to be generated dynamically so the catering company could use a web based front end to change their menu whenever they wanted. It's a bit involved but if anyone is ever doing anything similar and can't work it out from here, mail me and I will fill you in on the tricks. |
Lo-Fi Version | Time is now: 26th April 2024 - 08:30 AM |