PHP Form Email

This is a discussion on "PHP Form Email" within the PHP Forum section. This forum, and the thread "PHP Form 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 21st, 2008, 20:32
Up'n'Coming Member
Join Date: Jan 2008
Location: London
Age: 18
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Form Email

Hi guys,

Ive got this form (HTML) here:
http://southernheavenflorida.com/bookingform.html

I need it to send all the information inputted, to an email address.

Ive tried a few times and its just getting on my nerves.

Any help would be great, thanks in advance.

~Dean
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 21st, 2008, 20:34
Jack Franklin's Avatar
Moderator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,409
Blog Entries: 8
Thanks: 18
Thanked 14 Times in 14 Posts
Re: PHP Form Email

Can we see the PHP code pleasE?
__________________
Jack Franklin - Webforumz Moderator
(x)HTML | CSS | PHP | MySQL | JQuery (Javascript)
Contact: My Blog | Twitter | Delicious
Want Lessons? PM me.
If you think I've helped, please press the 'Thanks' Button.
Last Blog Entry: A Week with VBulletin (Aug 28th, 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 21st, 2008, 20:35
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 Email

I had one, but i broke it.
This isnt finished, i was editing it from a previous form.
Here:
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($_POST)>0) { // was something posted?
    
foreach($_POST as $key=>$value) { // loop through the $_POST array
  
if(in_array($key,$required_fields) && empty($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>
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 21st, 2008, 23:13
Reputable Member
Join Date: Jun 2007
Location: uk
Posts: 459
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Form Email

Hi

try changing this line

PHP: Select all

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

to
PHP: Select all

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

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 21st, 2008, 23:18
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 Email

Still got that error:
Parse error: syntax error, unexpected T_IF in /home/bravows1/public_html/southernheavenflorida.com/bookingform.php on line 23

Dam.

~Dean
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 21st, 2008, 23:26
Reputable Member
Join Date: Jun 2007
Location: uk
Posts: 459
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Form Email

Ok
Line 12 was first error

change

PHP: Select all

array_push($invalid_fields,'email'); 

to

PHP: Select all

array_push($invalid_fields); 

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 21st, 2008, 23:31
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 Email

I edited & uploaded the new file.

Says error on line 23 for me?

~Dean
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 21st, 2008, 23:34
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 Email

Can you post lines 21 - 25 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
  #9  
Old Jan 21st, 2008, 23:34
Reputable Member
Join Date: Jun 2007
Location: uk
Posts: 459
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Form Email

try copying below 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($_POST ='0') { // was something posted?
   
foreach($_POST as $key=>$value) { // loop through the $_POST array
  
if(in_array($key,$required_fields) && empty($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;
}
 
?>
It is working for me.
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 21st, 2008, 23:36
Reputable Member
Join Date: Jun 2007
Location: uk
Posts: 459
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Form Email

if that doesnt work try this

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($_POST ='0') { // was something posted?
   
foreach($_POST as $key=>$value) { // loop through the $_POST array
  
if(in_array($key,$required_fields) && empty($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;
}
 
?>
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 21st, 2008, 23:37
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 Email

Try replacing:
PHP: Select all

 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;
                } 
with:
PHP: Select all


 
if($key == '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>';
                        }
                } 
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
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP email form not sending email Kurt PHP Forum 1 Oct 12th, 2007 04:26
Form submits to email via php, but email is blank!!?? DH1234 PHP Forum 2 Jun 18th, 2007 10:42
HELP ME! Email form bruno89 Web Page Design 2 Apr 25th, 2006 17:42
Form to Email daveycee Web Page Design 3 Mar 16th, 2006 21:25
php email form djme PHP Forum 3 Jan 1st, 2006 22:06


All times are GMT. The time now is 20:34.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved