...

View Full Version : Email form?



new_at_this
07-04-2005, 11:43 PM
Hi,
Does anyone know of an "action=.PHP" (method="POST") code that with send the input results in an email (as text and you can name the subject of the email) to my email and when you click send, it sends it and takes you to a thank you page?

I have the form all made, I just need the PHP script to send it! :o

Thanks! :)

Serex
07-05-2005, 12:16 AM
if ($_POST['Submit'])
{
// email data here
// $var_name = $_POST['form_object_name'];

$your_email = "you@provider.com";
$subject = "Whatever You Want";

$f_name = $_POST['f_name'];
$l_name = $_POST['l_name'];
$email = $_POST['txtEmail'];
$other = $_POST['other'];

$body = "First Name: " . $f_name . "<br>" .
"Last Name: " . $l_name . "<br>" .
"Email: " . $email . "<br>" .
"<br>" .
"Other: <br><br>" .
$other;

if (!isset($f_name) || !isset($l_name) || !isset($email) || !isset($other))
{
// check too see that all fields were entered
echo "Not all fields filled out";
}
else
{
mail($your_email, $subject, $body, "From: \"$f_name $l_name\" <$email>\nContent-Type: text/html; charset=iso-8859-1");
echo "Thankyou, your data has been submitted. We will contact you as soon as possible.";
}

}

Ok. You can add the form action to whatever you like. lets say email.php. in email.php you can add the above, change all necessary fields, i just used some example ones. just woke up so if you have any problems post back and ill fix it

new_at_this
07-05-2005, 01:36 AM
Thank you so so So much Serex!!!!! I tried it and it doesn't really work for me (and I changed the email to mine). :(

Is it ok to remove:


// check too see that all fields were entered
echo "Not all fields filled out";So that it will send the form, even if some one doesn't fill in every filled?

Is there something I need to do to the HTML code to make it work?

Here's the HTML code:

<form method="POST" action=" the php code .php">

Your First Name:<BR>
<input name="Your First Name:" type="text" id="Your First Name:" value="" size="38" maxlength="100" class="fieldstyle">
<BR>
Your E-mail Address:<BR>
<input input name="Your E-mail Address:" type="text" id="Your E-mail Address:" size="38" maxlength="100" class="fieldstyle"">
<BR>
Your Website URL:</b><BR>
<input name="Your Website URL:" type="text" id="Your Website URL:" value="http://" size="38" maxlength="100" class="fieldstyle" value="http://" size="50">
<BR>
Your Favorite Thing:<BR>
<input name="Your Favorite Thing:" type="text" id="q[3]" value="" size="38" maxlength="100" class="fieldstyle" value="http://" size="50">
<BR>
The Country You Live In:<BR>
<input name="The Country You Live In:" type="text" id="The Country You Live In:" value="" size="38" maxlength="100" class="fieldstyle" value="http://" size="50">
<BR>
Your Comments:<BR>
<textarea input name="Your Comments:" type="text" id="Your Commentz:" size="30" rows="5" cols="40" wrap="virtual" maxlength="100" class="fieldstyle"></textarea>
<BR>
<input name="submit" type="submit" value="Join">
<input name="reset" type="reset" value="Clear">

</FORM>

And how do I get the code to take you to a HTML thank you page after you click send?

Thank you again so much! :D

Serex
07-05-2005, 03:22 AM
Ok i see a few errors with your above form (i.e duplication with some fields, invalid naming ect) i fixed it up and came up with.



<form name="form1" method="post" action="thankyou.php">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="12%">First Name </td>
<td>
<input name="name" type="text" id="name" size="38" maxlength="100">
</td>
</tr>
<tr>
<td>Email Address </td>
<td><input name="email" type="text" id="email" size="38" maxlength="100"></td>
</tr>
<tr>
<td>Website</td>
<td><input name="website" type="text" id="website" value="http://" size="38" maxlength="100"></td>
</tr>
<tr>
<td>Fabourite Thing </td>
<td><input name="textfield" type="text" size="38" maxlength="100"></td>
</tr>
<tr>
<td>Country</td>
<td><input name="textfield2" type="text" size="38" maxlength="100"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td valign="top">Comments</td>
<td><textarea name="comments" cols="40" rows="5" id="comments"></textarea></td>
</tr>
<tr>
<td valign="top">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td valign="top"><input type="submit" name="Submit" value="Submit"></td>
<td><input name="Reset" type="reset" id="Reset" value="Reset"></td>
</tr>
</table>
</form>


So you would replace that with your above HTML. And then you create a seperate page called thankyou.php and in there you would place the following



<?php
if ($_POST['Submit'])
{
// email data here
// $var_name = $_POST['form_object_name'];

$your_email = "you@provider.com";
$subject = "Whatever You Want";

$name = $_POST['name'];
$email = $_POST['email'];
$website = $_POST['website'];
$comments = $_POST['comments'];
$fav_thing = $_POST['fav_thing'];
$country = $_POST['country'];

$body = "<strong>First Name:</strong> " . $name . "<br>" .
"<strong>Email Address:</strong> " . $email . "<br>" .
"<strong>Country:</strong> " . $country . "<br>" .
"<strong>Website: </strong>" . $website . "<br>" .
"<strong>Favourite Thing: </strong>" . $fav_thing . "<br>" .
"<br>" .
"<strong>Comments:</strong> <br>" . $comments;

mail($your_email, $subject, $body, "From: \"$name\" <$email>\nContent-Type: text/html; charset=iso-8859-1");
echo "Thankyou, your data has been submitted. We will contact you as soon as possible.";
}
?>


any more questions. just ask

new_at_this
07-05-2005, 04:14 AM
OMG!!!!!!!!! THANK YOU SOOOOOOOO MUCH!!!!!!! You ROCK!!!! It works!!!! Yes!!!!!!!! :D :D :D

You have no idea how much this helps!! Yay!!! Thank you again so much!!!

new_at_this
07-06-2005, 01:47 PM
How do I make this work?:


$Body .= <strong>"Name:\n"</strong>;
$Body .= "$Name"<br>;
$Body .= <strong>"Email:\n"</strong>;
$Body .= "$EmailFrom"<br>;
$Body .= <strong>"Message:\n"</strong><br>;
$Body .= "$Message"<br>;

How do I get "Name:\n" in bold and underlined? and then have a <br> (or something) after it, in php?

So it turns out in the email like this?:
Name:
"$Name"
Email:
"$Email"
Message:
"$Message"

Thanks again!

XBleed
07-06-2005, 02:15 PM
How do I make this work?:


$Body .= <strong>"Name:\n"</strong>;
$Body .= "$Name"<br>;
$Body .= <strong>"Email:\n"</strong>;
$Body .= "$EmailFrom"<br>;
$Body .= <strong>"Message:\n"</strong><br>;
$Body .= "$Message"<br>;

How do I get "Name:\n" in bold and underlined? and then have a <br> (or something) after it, in php?

So it turns out in the email like this?:
Name:
"$Name"
Email:
"$Email"
Message:
"$Message"

Thanks again!



$Body .= <strong><u>"Name:\n"</u></strong><br />;

new_at_this
07-06-2005, 02:25 PM
I try this:


$Body .= <strong><u>"Name:\n"</u></strong><br />;
$Body .= "$Name"<br>;

And I get an error! :(

Serex
07-06-2005, 03:19 PM
$Body .= "<strong><u>Name:</u></strong><br>";
$Body .= $Name;


make sure your $Body is 'initialised'. Before you can use .= there needs to be a = somewere. ie:


$Body = "";
$Body .= "<strong><u>Name:</u></strong><br>";
$Body .= $Name;

new_at_this
07-06-2005, 06:38 PM
Thanks Serex! It worked! :)

I tested the form and it looks great!...
But does anyone know why, when someone doesn't enter an email address in the email input, the message doesn't send? An error doesn't come up or anything, the message just doesn't show up in my inbox. :(

I used to have an account at AllForms.com (http://allforms.mailjol.net/) and now it says:

"This Account Has Been Suspended"
"Please contact the billing/support department as soon as possible."

And I know it's not for me! I've tried to get to the site through another computer and got the same thing! I think somethings wrong with their server!?

On AllForms.com you could not enter an email, the form came from a no-respond email address, not from the person emailing through the form!
But now because I'm not using AllForms.com, the emails come from the persons email! But what if that person doesn't have an email?
Is there a way I can have the forms come from my email and not the email entered? I know it sounds weird, but is it possible? :o

Thanks! :D

AaronW
07-06-2005, 07:55 PM
This one's more re-usable without much customization: it gathers the list of variables and just e-mails the whole thing to you in a two-columned table of "Property | Value".



<?php
// Configuration
$method = $_POST; // $_GET or $_POST
$to = 'you@domain.com'; // Send e-mail to this address
$from = $method['email']; // 'From:' line
$subject = 'Subject'; // Subject of e-mail
$send_to = 'success.php'; // Where to send them afterwards

// Package and mail it
function to_rows ($array)
{
$str = '';
foreach ($_POST as $key => $value)
{
// Turn the field name into something legible
$key = ucwords (preg_replace (array ('/([a-z])([A-Z])/', '/[_-]/'), array ('\1 \2', ' '), $key));

// If this is an array[] field, recurse
if (is_array ($value))
{
$str .= to_rows ($value);
}
else
{
$str .= '
<tr>
<th scope="row">'.$key.'</th>
<td>'.$value.'</td>
</tr>
';
}
}
return $str;
}

$body = '
<html>
<head>
<title>Form Results</title>
</head>
<body>
<table>
<thead>
<th>Property</th>
<th>Value</th>
</thead>
<tbody>
'.to_rows ($method).'
</tbody>
</table>
</body>
</html>
';

$headers = '
MIME-Version: 1.0
From: '.$from.'
X-Mailer: MyMailer v1.1
X-Priority: 1
X-MSMail-Priority: High
Content-Type: text/html;charset=iso-8859-1
';

mail ($to, $subject, $body, $headers);
?>

new_at_this
07-06-2005, 08:10 PM
Thanks AaronW! I have the form all ready to use, I just need to know this:

If you don't enter an email address in the email input, the form isn't sent! What if someone doesn't have an email address? Is there any way the forms can be sent even without an email entered in the email input? :o Like from my address? ;)

Thanks! :)

new_at_this
07-06-2005, 09:06 PM
Will this work?:


mail($subject, $body, "From: \"$name \" < my email here >\nContent-Type: text/html; charset=iso-8859-1");

So that the form comes from my email address?

delinear
07-06-2005, 09:10 PM
Where you declare the $from field - just insert a check to see if the field is empty, if it is then fill it with the email of your choice:

$from = $method['email']; // 'From:' line
if(empty($from)) {
$from = 'your@email.address';
}
That should do it. Technically you shouldn't need a from address but I guess it doesn't like it when the from header is there but not completed.

new_at_this
07-06-2005, 09:21 PM
Ok, I'm really need at PHP and used to HTML!

So add ad that here?:


<?php
if ($_POST['Submit'])
{
// email data here
// $var_name = $_POST['form_object_name'];

$your_email = " my email";
$subject = "New Message";

$name = $_POST['name'];
$email = $_POST['email'];
$website = $_POST['website'];
$favthing = $_POST['favthing'];
$country = $_POST['country'];

$body = "<strong><u>Your First Name:</u></strong><br>" . $name . "<br><br>" .
"<strong><u>Your E-mail Address:</u></strong><br>" . $email . "<br><br>" .
"<strong><u>Your Website URL:</u></strong><br>" . $website . "<br><br>" .
"<strong><u>Your Favorite Thing:</u></strong><br>" . $favthing . "<br><br>" .
"<strong><u>The Country You Live In:</u></strong><br>" . $country . "<br><br>" .
"<strong><u>IP Address:</u></strong> <br>" . $REMOTE_ADDR .
"<br><br><br><br><br><br>" .
"<strong>_______________________________________________</strong>" .
"<br><br><br>";

header("location: http://");
mail($your_email, $subject, $body, "From: \"$name \" <$email>\nContent-Type: text/html; charset=iso-8859-1");

$from = $method['$email']; // 'From:' line
if(empty($from)) {
$from = ' my email ';
}
?>

I like the form the way it is, I just need to know, do I have the bottom part right? Sorry! :o

AaronW
07-06-2005, 09:33 PM
If you use mine, they won't need to supply an e-mail address.

new_at_this
07-06-2005, 09:38 PM
Ok, but where do I put it in my code? And what about the people who do want their emails entered?

anarchy3200
07-06-2005, 09:50 PM
Using them pieces of code, it should become:




<?php
if ($_POST['Submit'])
{
// email data here
// $var_name = $_POST['form_object_name'];

$your_email = " my email";
$subject = "New Message";

$name = $_POST['name'];
$email = $_POST['email']; // 'From:' line INSERTED HERE
if(empty($email)) {
$email= 'email@not.set';
}
$website = $_POST['website'];
$favthing = $_POST['favthing'];
$country = $_POST['country'];

$body = "<strong><u>Your First Name:</u></strong><br>" . $name . "<br><br>" .
"<strong><u>Your E-mail Address:</u></strong><br>" . $email . "<br><br>" .
"<strong><u>Your Website URL:</u></strong><br>" . $website . "<br><br>" .
"<strong><u>Your Favorite Thing:</u></strong><br>" . $favthing . "<br><br>" .
"<strong><u>The Country You Live In:</u></strong><br>" . $country . "<br><br>" .
"<strong><u>IP Address:</u></strong> <br>" . $REMOTE_ADDR .
"<br><br><br><br><br><br>" .
"<strong>_______________________________________________</strong>" .
"<br><br><br>";

header("location: http://");
mail($your_email, $subject, $body, "From: \"$name \" <$email>\nContent-Type: text/html; charset=iso-8859-1");


?>

new_at_this
07-06-2005, 10:28 PM
Yes!!! It works!!! Thank you all so so much, you ROCK! I love this fourm!!! :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum