Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-04-2005, 11:43 PM   PM User | #1
new_at_this
New Coder

 
Join Date: Jun 2005
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
new_at_this is an unknown quantity at this point
Email form?

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!

Thanks!
new_at_this is offline   Reply With Quote
Old 07-05-2005, 12:16 AM   PM User | #2
Serex
Regular Coder

 
Join Date: Mar 2004
Location: Australia
Posts: 217
Thanks: 0
Thanked 1 Time in 1 Post
Serex is an unknown quantity at this point
PHP Code:
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
Serex is offline   Reply With Quote
Old 07-05-2005, 01:36 AM   PM User | #3
new_at_this
New Coder

 
Join Date: Jun 2005
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
new_at_this is an unknown quantity at this point
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:
PHP Code:
// 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:
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!
new_at_this is offline   Reply With Quote
Old 07-05-2005, 03:22 AM   PM User | #4
Serex
Regular Coder

 
Join Date: Mar 2004
Location: Australia
Posts: 217
Thanks: 0
Thanked 1 Time in 1 Post
Serex is an unknown quantity at this point
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.

PHP Code:
<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 Code:
<?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

Last edited by Serex; 07-05-2005 at 06:17 AM..
Serex is offline   Reply With Quote
Old 07-05-2005, 04:14 AM   PM User | #5
new_at_this
New Coder

 
Join Date: Jun 2005
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
new_at_this is an unknown quantity at this point
OMG!!!!!!!!! THANK YOU SOOOOOOOO MUCH!!!!!!! You ROCK!!!! It works!!!! Yes!!!!!!!!

You have no idea how much this helps!! Yay!!! Thank you again so much!!!
new_at_this is offline   Reply With Quote
Old 07-06-2005, 01:47 PM   PM User | #6
new_at_this
New Coder

 
Join Date: Jun 2005
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
new_at_this is an unknown quantity at this point
How do I make this work?:

PHP Code:
$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!
new_at_this is offline   Reply With Quote
Old 07-06-2005, 02:15 PM   PM User | #7
XBleed
New to the CF scene

 
Join Date: Jul 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
XBleed is an unknown quantity at this point
Quote:
Originally Posted by new_at_this
How do I make this work?:

PHP Code:
$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!

PHP Code:
$Body .= <strong><u>"Name:\n"</u></strong><br />; 
XBleed is offline   Reply With Quote
Old 07-06-2005, 02:25 PM   PM User | #8
new_at_this
New Coder

 
Join Date: Jun 2005
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
new_at_this is an unknown quantity at this point
I try this:

PHP Code:
$Body .= <strong><u>"Name:\n"</u></strong><br />;
$Body .= "$Name"<br>; 
And I get an error!
new_at_this is offline   Reply With Quote
Old 07-06-2005, 03:19 PM   PM User | #9
Serex
Regular Coder

 
Join Date: Mar 2004
Location: Australia
Posts: 217
Thanks: 0
Thanked 1 Time in 1 Post
Serex is an unknown quantity at this point
PHP Code:
$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:
PHP Code:
$Body "";
$Body .= "<strong><u>Name:</u></strong><br>";
$Body .= $Name
Serex is offline   Reply With Quote
Old 07-06-2005, 06:38 PM   PM User | #10
new_at_this
New Coder

 
Join Date: Jun 2005
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
new_at_this is an unknown quantity at this point
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 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?

Thanks!
new_at_this is offline   Reply With Quote
Old 07-06-2005, 07:55 PM   PM User | #11
AaronW
Senior Coder

 
Join Date: Feb 2003
Location: Ontario, Canada
Posts: 1,223
Thanks: 0
Thanked 0 Times in 0 Posts
AaronW is an unknown quantity at this point
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 Code:
<?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);
?>
__________________
offtone.com | offtonedesign.com
AaronW is offline   Reply With Quote
Old 07-06-2005, 08:10 PM   PM User | #12
new_at_this
New Coder

 
Join Date: Jun 2005
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
new_at_this is an unknown quantity at this point
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? Like from my address?

Thanks!

Last edited by new_at_this; 07-06-2005 at 08:40 PM..
new_at_this is offline   Reply With Quote
Old 07-06-2005, 09:06 PM   PM User | #13
new_at_this
New Coder

 
Join Date: Jun 2005
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
new_at_this is an unknown quantity at this point
Will this work?:

PHP Code:
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?
new_at_this is offline   Reply With Quote
Old 07-06-2005, 09:10 PM   PM User | #14
delinear
Regular Coder

 
Join Date: Feb 2005
Location: West Midlands, UK
Posts: 623
Thanks: 0
Thanked 0 Times in 0 Posts
delinear is an unknown quantity at this point
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:
PHP Code:
$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.
__________________
~ Bazzy
delinear is offline   Reply With Quote
Old 07-06-2005, 09:21 PM   PM User | #15
new_at_this
New Coder

 
Join Date: Jun 2005
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
new_at_this is an unknown quantity at this point
Ok, I'm really need at PHP and used to HTML!

So add ad that here?:

PHP Code:
<?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!

Last edited by new_at_this; 07-06-2005 at 09:38 PM..
new_at_this is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:00 PM.


Advertisement
Log in to turn off these ads.