PHP Form send in email

This is a discussion on "PHP Form send in email" within the PHP Forum section. This forum, and the thread "PHP Form send in email are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Jan 16th, 2008, 20:56
Up'n'Coming Member
Join Date: Jan 2008
Location: London
Age: 18
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Form send in email

Hi everyone,

I need help in making a form, ive made the basic HTML sides of it.

I need the PHP side, which will send the data input to the HTML Form to an email.

Any help will be greatly appreciated as ive tried three times before and had errors.


The form can be found here:
www.southernheavenflorida.com/bookingform.html


Thanks in advance for any help!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Jan 16th, 2008, 20:58
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: PHP Form send in email

http://www.webforumz.com/php-forum/6...validating.htm
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Jan 16th, 2008, 21:00
Up'n'Coming Member
Join Date: Jan 2008
Location: London
Age: 18
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Form send in email

Sorry, didnt think of searching the forum before posting because it slipped my mind

Sorry!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Jan 16th, 2008, 21:03
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: PHP Form send in email

Don't worry mate
But check the stickies before posting, they can usually help a lot!
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Jan 16th, 2008, 21:03
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Form send in email

Yap, start by checking out my tutorial in the stickies.... Feel free to post any questions you may have!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Jan 16th, 2008, 21:28
Up'n'Coming Member
Join Date: Jan 2008
Location: London
Age: 18
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Form send in email

Ok, ive got an error.

if(count($_POST)>0) { // was something posted?

Thats the error line, the error is:
Parse error: syntax error, unexpected T_IF in /home/bravows1/public_html/southernheavenflorida.com/bookingform.php on line 12

Help please!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Jan 16th, 2008, 21:35
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: PHP Form send in email

Could you post the lines either side of that too please.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Jan 16th, 2008, 21:36
Up'n'Coming Member
Join Date: Jan 2008
Location: London
Age: 18
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Form send in email

Sure, all the coding is just copied from the last bit of the tutorial.
Slightly edited it.
Heres the code:
PHP: Select all

<?php
/****** PHP Form Mail *****/
// declare and initialize some variables
$name=$email=$subject=$message=$error_message='';
$invalid_fields=array();
$required_fields=array('name','email','message');
$validated=array();
$to_address='admin@bravo-webspace.co.uk'// your email address goes here
// validate $_POST array
if(count($_POST)>0) { // was something posted?
    
foreach($_POST as $key=>$value) { // loop through the $_POST array
        
if(in_array($key,$required_fields)&&$value=='') { // check if a required field is empty
             // add that field to the $invalid_fields array
            
array_push($invalid_fields,$key);
            
// and append the error message to the $error_message variable
            
$error_message.='<p>Please enter a'.(preg_match('/^[aeiouy]/',$key)?'n':'').' '.$key.'.</p>';
        } else {

Last edited by c010depunkk; Jan 17th, 2008 at 06:17. Reason: please use [PHP] tags when posting PHP
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old Jan 16th, 2008, 21:42
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: PHP Form send in email

That's odd, I can't spot an error there.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10  
Old Jan 16th, 2008, 21:44
Up'n'Coming Member
Join Date: Jan 2008
Location: London
Age: 18
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Form send in email

Thats what i thought.
Heres the whole file,

PHP: Select all

<?php
/****** PHP Form Mail *****/
// declare and initialize some variables
$name=$email=$subject=$message=$error_message='';
$invalid_fields=array();
$required_fields=array('name','email','message');
$validated=array();
$to_address='admin@bravo-webspace.co.uk'// your email address goes here
// validate $_POST array
if(count($_POST)>0) { // was something posted?
    
foreach($_POST as $key=>$value) { // loop through the $_POST array
        
if(in_array($key,$required_fields)&&$value=='') { // check if a required field is empty
             // add that field to the $invalid_fields array
            
array_push($invalid_fields,$key);
            
// and append the error message to the $error_message variable
            
$error_message.='<p>Please enter a'.(preg_match('/^[aeiouy]/',$key)?'n':'').' '.$key.'.</p>';
        } else {
            switch(
$key) {
                case 
'email'// validate email address format
                    
if(!preg_match('/^([A-Z0-9._%-]+)@([A-Z0-9.-]+)\.([A-Z]{2,6})$/i',$value)) {
                        
array_push($invalid_fields,'email');
                        
$error_message.='<p>Please enter a valid email.</p>';
                    }
                    break;
            }
        }
        
// field is not in the $invalid_fields array?
        
if(!in_array($key,$invalid_fields)) {
            
// copy it to the $validated array
            
$validated[$key]=htmlspecialchars($value);
        }
    }
} else { 
// make everything invalid so that the form is outputted and not the thankyou message
    
$invalid_fields=$required_fields;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtmlxml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Villa Booking Form</title>
<style type="text/css" media="all">
* { margin:0;padding:0; }
body { font:600 12px/22px verdana;padding:50px; }
.box, textarea, .button { border:2px solid #6699CC;font:inherit;color:inherit; }
.box { width:250px; }
textarea { width:300px;height:250px; }
.error { color:#DD1100; }
</style>
</head>
<body>
<div class="contact">
    <h1>Booking Form:</h1>
<?php
if(count($invalid_fields)<=0) { // send message / thank user
    
$formatted_message='When: '.date('r').'
Who: '
.$name.' ('.$email.')
With What: '
.$_SERVER['HTTP_USER_AGENT'].'
Message: '
.$message;
    
// send the email
    
mail($to_address,$subject,$formatted_message);
?>
    <p>Your message was successfully delivered.</p>
<?php } else { ?>
    <p>Please fill out the form to contact us. Required fields are marked with a star [*].</p>
    <?php echo(($error_message!=''?'<div class="error">'.$error_message.'</div>':'')); ?>
    <form name="contact" action="form_mail.php" method="post">
        <p>Name:* <input class="box" type="text" name="name" value="<?php echo($validated['name']); ?>" /></p>
        <p>E-Mail:* <input class="box" type="text" name="email" value="<?php echo($validated['email']); ?>" /></p>
        <p>Subject: <input class="box" type="text" name="subject" value="<?php echo($validated['subject']); ?>" /></p>
        <p>Message:*</p><textarea name="message"><?php echo($validated['message']); ?></textarea>
        <p><input class="button" type="submit" action="submit" value="Send" />
    </form>
<?php ?>
</div>
</body>
</html>
Im baffled by this as i just copied the tut..

Thanks for your help Alex.

Last edited by c010depunkk; Jan 17th, 2008 at 06:18. Reason: please use [PHP] tags when posting PHP
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #11  
Old Jan 16th, 2008, 21:47
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: PHP Form send in email

Try replacing
PHP: Select all

if(count($_POST)>0) { // was something posted? 

With just this:
PHP: Select all

if($_POST)) { 

Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #12  
Old Jan 16th, 2008, 21:49
Up'n'Coming Member
Join Date: Jan 2008
Location: London
Age: 18
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Form send in email

Same error, the if error. Dam
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #13  
Old Jan 16th, 2008, 21:54
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: PHP Form send in email

Actually, line 12 for me is:
PHP: Select all

if(in_array($key,$required_fields)&&$value=='') { // check if a required field is empty 

Try this instead:
PHP: Select all

if(in_array($key,$required_fields) && empty($value)) { 

Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #14  
Old Jan 16th, 2008, 21:57
Up'n'Coming Member
Join Date: Jan 2008
Location: London
Age: 18
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Form send in email

Same damn error!

What gives?

Its erroring on line 12:
if($_POST)>0) { // was something posted?

The only code above that is:

<?php
/****** PHP Form Mail *****/
// declare and initialize some variables
$name=$email=$subject=$message=$error_message='';
$invalid_fields=array();
$required_fields=array('name','email','message');
$validated=array();
$to_address='admin@bravo-webspace.co.uk'; // your email address goes here

But thats all correct?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #15  
Old Jan 16th, 2008, 22:01
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: PHP Form send in email

Maybe Jan (c010depunkk) can help. He wrote the tutorial after all.
I have no idea why that is happening!
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote