Welcome to WebmasterWorld Guest from 54.145.53.251

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Form to PHP Script - Email

Here we go...

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 15, 2002
posts:971
votes: 0


Have an easy one for you...

Just taking this example:

<?php
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;)

Thanks-

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 6, 2002
posts:1092
votes: 0


<?php

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

if($something=="yes"){
$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 Sept 29, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 15, 2002
posts:971
votes: 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 Sept 29, 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


depending on your version of php

php version > 4.1.0
$_POST["level"]

or

php version < 4.1.0
$HTTP_POST_VARS["level"]

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 15, 2002
posts:971
votes: 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 Sept 30, 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


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
mail($emailto,$subject,$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 Sept 30, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 22, 2002
posts:1782
votes: 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",
$nice_name[$key],
is_array($value)? implode(', ', $value) : $value);
}

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

Andreas

1:15 am on Sept 30, 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


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 Sept 30, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 15, 2002
posts:971
votes: 0


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

M