homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Form to PHP Script - Email
Here we go...

 10:57 pm on Sep 29, 2002 (gmt 0)

Have an easy one for you...

Just taking this example:

mail('recipient@some.net', 'Subject', 'Your message here.');

I have a mixture of form tables that use checkboxes, radio buttons, textareas and the whole nine yards.

How do I format all of the information I have in the form and send it in the mail function?

It seems like I could just take something like...

$message = ...(all the info)

-->but I'm not sure how to transport it all into the variable. Honestly hope that makes sense, very unclear at the moment;)




 11:04 pm on Sep 29, 2002 (gmt 0)


$message = "Var 1: $var1\nVar 2:$var2";

$message .= "\nVar 3: $var3";

mail('recipient@some.net', 'Subject', $message);

This would do.... Or do you not know the variable names ahead of time?


 11:18 pm on Sep 29, 2002 (gmt 0)

Hey thanks Lisa- I know everything ahead of time...

<input type="radio" name="level"
value="start_novice" />
<input type="radio" name="level" value="start_inter" />
<input type="radio" name="level" value="start_expert" />

for example...I would need the value chosen from the level array- into the $message variable along with similar form info.


 11:38 pm on Sep 29, 2002 (gmt 0)

depending on your version of php

php version > 4.1.0


php version < 4.1.0


 12:18 am on Sep 30, 2002 (gmt 0)

$message = $_POST["level"]

Do you mean like this? If I had other arrays like this would it just be a comma separated list or a semi-colon separated list? That's probably thick;)

Also, in the subject of the email response-

$subject = "HelpDesk Inquiry";

--> Can you throw in a date fuction after inquiry so that the time shows up in the email title?

Thanks again!


 12:28 am on Sep 30, 2002 (gmt 0)

the $_POST (or $HTTP_POST_VARS) array contains all of the values from your form as long as you used METHOD="POST" in your form tag. In the case of a multiple choice element it will contain the value of the selected item.

You do exactly what Lisa showed you but you use the vars that I gave you instead of the generic style ones.

here is a baby template but you will have to do some yourself

$emailto = "address@somewhere.com";
$subject = "HelpDesk Inquiry";
//construct your message here

$message = $_POST["level"];
$message .= $_POST["anotherVar"]

//end message

That should give you a good idea of how to do it. Your message line can be a function, a loop, multiple functions. You can add a date function in there.

php.net is your friend ;)


 1:02 am on Sep 30, 2002 (gmt 0)

Your message line can be a function, a loop, multiple functions.

To be precise your message parameter needs to be a string [php.net].

If you use a form like the one in Example 8-3. More complex form variables [php.net] building your message string is as easy as looping through the $form array. Unlike that example I would name the beer select form[beer][] to stay true to the naming scheme. And you should use the auto-global $_POST instead of $HTTP_POST_VARS.

If you have an additional array mapping your keys to nice name you can make processing even easier. In the aforementioned example there are the keys name, email and beer. Suppose you have an array like $nice_names = array(name => 'Customerīs name', email => 'Email address', beer => 'Favorite beer'), then you could do the following:

foreach($form as $key => $value) { 
$message .= sprintf("%s: $s\n",
is_array($value)? implode(', ', $value) : $value);

Customerīs name: Aaron
Email address: aaron@domain.tld
Favorite beer: warthog, guinness, stuttgarter



 1:15 am on Sep 30, 2002 (gmt 0)

Your message line can be a function, a loop, multiple functions.

sorry, more precisely.

To construct your message string you can use a function, a loop, multiple functions.

I most often use foreach loops to go through the fields and format them.


 2:27 pm on Sep 30, 2002 (gmt 0)

Thank you for your help- that gives me a great starting place.


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