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.



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

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Jan 16th, 2008, 20:56
Up'n'Coming Member
Join Date: Jan 2008
Location: London
Age: 17
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!
Reply With Quote

  #2 (permalink)  
Old Jan 16th, 2008, 20:58
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: PHP Form send in email

PHP mail() --> Making a Form / Validating Input / Sending an email
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Reply With Quote
  #3 (permalink)  
Old Jan 16th, 2008, 21:00
Up'n'Coming Member
Join Date: Jan 2008
Location: London
Age: 17
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!!
Reply With Quote
  #4 (permalink)  
Old Jan 16th, 2008, 21:03
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
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)
Reply With Quote
  #5 (permalink)  
Old Jan 16th, 2008, 21:03
c010depunkk's Avatar
SuperMember

SuperMember
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to c010depunkk
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!
Reply With Quote
  #6 (permalink)  
Old Jan 16th, 2008, 21:28
Up'n'Coming Member
Join Date: Jan 2008
Location: London
Age: 17
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!
Reply With Quote
  #7 (permalink)  
Old Jan 16th, 2008, 21:35
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
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)
Reply With Quote
  #8 (permalink)  
Old Jan 16th, 2008, 21:36
Up'n'Coming Member
Join Date: Jan 2008
Location: London
Age: 17
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
Reply With Quote
  #9 (permalink)  
Old Jan 16th, 2008, 21:42
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
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)
Reply With Quote
  #10 (permalink)  
Old Jan 16th, 2008, 21:44
Up'n'Coming Member
Join Date: Jan 2008
Location: London
Age: 17
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
Reply With Quote
  #11 (permalink)  
Old Jan 16th, 2008, 21:47
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
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)
Reply With Quote
  #12 (permalink)  
Old Jan 16th, 2008, 21:49
Up'n'Coming Member
Join Date: Jan 2008
Location: London
Age: 17
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Form send in email

Same error, the if error. Dam
Reply With Quote
  #13 (permalink)  
Old Jan 16th, 2008, 21:54
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
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)
Reply With Quote
  #14 (permalink)  
Old Jan 16th, 2008, 21:57
Up'n'Coming Member
Join Date: Jan 2008
Location: London
Age: 17
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?
Reply With Quote
  #15 (permalink)  
Old Jan 16th, 2008, 22:01
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
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)
Reply With Quote
  #16 (permalink)  
Old Jan 16th, 2008, 22:03
Up'n'Coming Member
Join Date: Jan 2008
Location: London
Age: 17
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Form send in email

Dam, ok Alex thanks for your help and fast replys.

i'l PM him.
Reply With Quote
  #17 (permalink)  
Old Jan 16th, 2008, 22:04
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: PHP Form send in email

I wouldn't, he discourages that I think.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Reply With Quote
  #18 (permalink)  
Old Jan 16th, 2008, 22:07
Up'n'Coming Member
Join Date: Jan 2008
Location: London
Age: 17
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Form send in email

Doh! wish you had posted that quicker.

I PM'd him telling him im having trouble, and asked him to check the topic when hes free.

O dear.
Reply With Quote
  #19 (permalink)  
Old Jan 16th, 2008, 22:17
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: PHP Form send in email

I'm sure he'll understand
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Reply With Quote
  #20 (permalink)  
Old Jan 17th, 2008, 06:45
c010depunkk's Avatar
SuperMember

SuperMember
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to c010depunkk
Re: PHP Form send in email

Quote:
Originally Posted by alexgeek View Post
Maybe Jan (c010depunkk) can help. He wrote the tutorial after all.
I have no idea why that is happening!
I'll try
Quote:
Originally Posted by alexgeek View Post
I wouldn't, he discourages that I think.
Well, I don't mind you PM'ing me to hurry up and look. It just won't change anything !!! I just don't like it when people PM me with web-development-related questions.... That's what the forums are for....
Quote:
Originally Posted by alexgeek View Post
I'm sure he'll understand
This time he did....


OK, now the error:

I'm getting the same error, no clue why.... Must have something to do with the encoding - it gets broken when you use the [select all] feature (I'll report that to Jacob).... There are no syntax errors. I played around a bit with converting the encoding to UTF-8 and then back to ANSI but nothing seems to solve the problem, HOWEVER....

I edited the tutorial and attached the complete code in a PHP file. I'll do the same on this post. When I downloaded the file and tested it, it worked for me. So, check that out and tell us what happened
Attached Files
File Type: php form_mail.php (3.1 KB, 4 views)

Last edited by c010depunkk; Jan 17th, 2008 at 06:46. Reason: sp
Reply With Quote
Reply

Thread Tools
Show Printable Version