Sending all Form Input

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Zoram
Forum Contributor
Posts: 166
Joined: Sun Aug 18, 2002 3:28 pm
Location: Utah
Contact:

Sending all Form Input

Post by Zoram »

I'm trying to figure out how i can compile all the input from a form and send it using the mail function. I've got a form setup to send to the script but i'm not sure how to setup the script to mail everything submitted.

Thanks
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Code: Select all

<?php

$username = $_POSTї"username"];
$email = $_POSTї"email"];

$message = 
"Username: $username
E-mail address: $email";

mail("TO ADDRESS","SUBJECT","MESSAGE","From: FROM ADDRESS");
?>
If you have two text field in the form
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

here is a basic mail script

Code: Select all

<?php

// set the address the mailform will be sent to
$toaddress = "plush@yoursite.com";

// type in the subject of the email
$subject = "FREE CD REQUEST";

// here is where you have your fields you collected from your form
$mailcontent = "Name: ".$name."\n"
			   ."Email: ".$email."\n"
			   ."Add1: ".$add1."\n"
			   ."Add2: ".$add2."\n"
			   ."City: ".$city."\n"
			   ."State: ".$state."\n"
		                   ."Zip: ".$zip."\n"
		                 
	
// set the from address which is what the email will say "SENT FROM"
$fromaddress = "info@yoursite.com";

// finally send off the email in the proper form
mail($toaddress, $subject, $mailcontent, $fromaddress);
?>
there is also a few other things for sending an email in HTML format but this is a simple text email
User avatar
Zoram
Forum Contributor
Posts: 166
Joined: Sun Aug 18, 2002 3:28 pm
Location: Utah
Contact:

Post by Zoram »

What about when i can't predict how many form elements are being passed in? I am making a contest page for my site and to be entered into the contest the user checks the checkbox by the contest and then fill out the submit info. The only constant thing is all of it is the user info. Everything else id dynamic.
kcomer
Forum Contributor
Posts: 108
Joined: Tue Aug 27, 2002 8:50 am

Post by kcomer »

You could always loop through the $_POST array. That will give you all the values from the form, even hidden ones. It may work for what you are doing.

foreach($_POST as $key=>$value) {
print "$key: $value";
}

Just mix it in with your mail script.

Keith
User avatar
Zoram
Forum Contributor
Posts: 166
Joined: Sun Aug 18, 2002 3:28 pm
Location: Utah
Contact:

Post by Zoram »

How would the $key and $value variable catch all the values?
kcomer wrote:You could always loop through the $_POST array. That will give you all the values from the form, even hidden ones. It may work for what you are doing.

foreach($_POST as $key=>$value) {
print "$key: $value";
}

Just mix it in with your mail script.

Keith
kcomer
Forum Contributor
Posts: 108
Joined: Tue Aug 27, 2002 8:50 am

Post by kcomer »

$_POST is an array that holds every single key/variable pair that was submited by the previouse form. If you print all of those then you have all the forms values. Also, depending on your version you may have to use $_HTTP_POST_VARS. As always, read the php manual, its better than any book or message board.

run this code on the page that processes the form, it will show you what $_POST is holding. Or any other array for that matter.

<?
print "<pre>";
print_r($_POST);
print "</pre>";

Keith
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

try something like:

Code: Select all

foreach($_POST as $key => $value)&#123;
$message .= $key." : ".$value."/n";
&#125;
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post by Coco »

*copy from jplush*

Code: Select all

&lt;?php

// set the address the mailform will be sent to 
$toaddress = "plush@yoursite.com"; 

// type in the subject of the email 
$subject = "FREE CD REQUEST"; 

// here is where you have your fields you collected from your form 
$mailcontent = "Name: ".$name."\n"

if ($email!='')
           $mailcontent = $mailcontent ."Email: ".$email."\n";
if ($nextvalue!='')
           //ETC;
// set the from address which is what the email will say "SENT FROM" 
$fromaddress = "info@yoursite.com"; 

// finally send off the email in the proper form 
mail($toaddress, $subject, $mailcontent, $fromaddress); 
?&gt; 


?&gt;
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Put it all together like this:-

Code: Select all

&lt;?php

$toaddress = "mail@mail.com"; //E-mail address that you are sending
$subject = "Subject of e-mail";
$fromaddress = mail2@mail.com";  //The e-mail address of the sender

foreach($_POST as $key =&gt; $value){
  $message .= $key." : ".$value."\n";
}

mail($toaddress,$subject,$message,$fromaddress);
?&gt;
Post Reply