[SOLVED] PHP Contact Form

This is a discussion on "[SOLVED] PHP Contact Form" within the PHP Forum section. This forum, and the thread "[SOLVED] PHP Contact Form 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




Closed Thread
 
LinkBack Thread Tools
  #1  
Old Oct 13th, 2007, 13:02
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] PHP Contact Form

Hello,

I am new to php and am having a slight problem. The contact form I have on my website is submitted through php. When the user clicks 'Submit', the send.php page displays a message which tells the user if the form was submitted correctly or if s/he entered something wrong. It works fine except that all the HTML after the php code is missing. In other words, the php overwrites anything that comes after it.

My code is:
PHP: Select all

<?php

$name 
$_POST['name'];
$email $_POST['email'];
$type $_POST['type'];
$url $_POST['url'];
$message $_POST['message'];
$ip $_POST['ip'];
$browser $_POST['browser'];


if (
eregi('http:'$message)) {
echo 
"<p align=\"center\" class=\"style1\">Please enter the http: address into the URL field</p>";
exit (
"<p align=\"center\" class=\"style2\"><a href=\"javascript:history.back();\">Return to form</a></p>");
}
if(!
$email == "" && (!strstr($email,"@") || !strstr($email,".")))
{
echo 
"<p align=\"center\" class=\"style1\">Invalid email address entered";
$badinput "Please try again</p>\n";
echo 
$badinput;
exit (
"<p align=\"center\" class=\"style2\"><a href=\"javascript:history.back();\">Return to form</a></p>");
}

if(empty(
$name) || empty($email) || empty($message )) {
echo 
"<p align=\"center\" class=\"style1\">Please fill in all required fields</p>\n";
exit (
"<p align=\"center\" class=\"style2\"><a href=\"javascript:history.back();\">Return to form</a></p>\n");
}

$today date("l, F j, Y, g:i a") ;

$type $type ;
$subject "Contact the Webmaster";

$message stripcslashes($message);

$message "Date sent: $today [EST] \n
From: $name ($email)\n
URL : $url \n
Message Type: $type \n
Message: $message \n
User IP: = $ip \n
Browser Info: $browser \n
"
;

$from "From: $email\r\n";


mail("MyEmail@yahoo.com"$subject$message$from);

?>

<p align="center" class="style1">Your message has been submitted successfully...</p>
<p align="center" class="style2">Thank you for helping us improve our website</p>
The two <p> tags at the end are the message that is displayed if there are no errors in the form. But everything after those <p> tags is completely gone...

Any help would be much appreciated.

Thanks,
SWagner
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!

  #2  
Old Oct 13th, 2007, 13:12
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Contact Form

It's because you're calling the exit() function on an error, this stops the script and won't put anything else out.

Or do you mean that it won't display when everything validates? If this is the case, try wrapping all the variables you use between double quotes ie. " in curly braces to make sure PHP isn't getting confused amongst the special characters.

PHP won't overwrite anything as it works from top to bottom, you may wish to check your error long to be sure there aren't any hard-to-see sytax errors.

Let me know how you go.

Cheers,
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #3  
Old Oct 13th, 2007, 19:38
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Contact Form

OK, I see what you mean. However, the exit(), or die(), function is needed so that the incomplete email is not sent to me. How do I still use the exit()/die() function without the rest of my page being cut off?
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)

Last edited by Stuart; Oct 13th, 2007 at 20:21.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #4  
Old Oct 14th, 2007, 00:24
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Contact Form

Okay, instead of exiting on error, just store the errors in an array to output. This will allow you to check error before sending mail...

PHP: Select all

<?php


$name
*=*$_POST['name'];
$email*=*$_POST['email'];
$type*=*$_POST['type'];
$url*=*$_POST['url'];
$message*=*$_POST['message'];
$ip*=*$_POST['ip'];
$browser*=*$_POST['browser'];

$errors = array();  // start an empty array


if*(eregi('http:',*$message))*{
$errors[] = *"<p*align=\"center\"*class=\"style1\">Please*enter*the*http:*address*into*the*URL*field</p>";
}
if(!
$email*==*""*&&*(!strstr($email,"@")*||*!strstr($email,".")))
{
$errors[] = *"<p*align=\"center\"*class=\"style1\">Invalid*email*address*entered</p>";
}

if(empty(
$name)*||*empty($email)*||*empty($message*))*{
$errors[] = *"<p*align=\"center\"*class=\"style1\">Please*fill*in*all*required*fields</p>\n";

}

// Check for errors before sending email

if (!count($errors)) 
{


$today*=*date("l,*F*j,*Y,*g:i*a")*;

$type*=*$type*;
$subject*=*"Contact*the*Webmaster";

$message*=*stripcslashes($message);

$message*=*"Date*sent:*$today*[EST]*\n
From:*$name*($email)\n
URL*:*$url*\n
Message*Type:*$type*\n
Message:*$message*\n
User*IP:*=*$ip*\n
Browser*Info:*$browser*\n
"
;

$from*=*"From:*$email\r\n";


mail("MyEmail@yahoo.com",*$subject,*$message,*$from);
} else {

// now output the errors for user to see

foreach ($errors as $msg
     echo 
$msg;

echo 
'<p*align=\"center\"*class=\"style2\"><a*href=\"javascript:history.back();\">Return*to*form</a></p>';

}


?>

<p*align="center"*class="style1">Your*message*has*been*submitted*successfully...</p>
<p*align="center"*class="style2">Thank*you*for*helping*us*improve*our*website</p>
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #5  
Old Oct 14th, 2007, 00:53
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Contact Form

Thanks. It works great.
SWagner

PS: Why did you replace almost every space with a '*'?
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #6  
Old Oct 14th, 2007, 01:04
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Contact Form

I didn't put the asterix there

That's really weird.. sorry about that.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #7  
Old Oct 14th, 2007, 01:14
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Contact Form

That's ok. I just used the 'Find and Replace' option in Dreamweaver to replace any asterisk with a space...
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #8  
Old Oct 19th, 2007, 00:46
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Contact Form

Also, I have several checkboxes in my form. The problem is that only one of the values (if more than one is checked) is sent to me by email. Why is that, and how do I fix it?
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #9  
Old Oct 19th, 2007, 01:58
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Contact Form

Checkboxes all have to have a different name or else the last one will overwrite the previous one.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #10  
Old Oct 20th, 2007, 00:48
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Contact Form

OK, thanks.
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Closed Thread

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
[SOLVED] PHP contact form problem fl0w PHP Forum 12 Jan 31st, 2008 17:56
[SOLVED] PHP contact form redirect to same form Posie PHP Forum 14 Jan 29th, 2008 20:28
[SOLVED] Wordpress contact form - getting 404 on send Aso PHP Forum 3 Jan 25th, 2008 11:23
[SOLVED] PHP Contact Form Stuart PHP Forum 8 Dec 4th, 2007 17:42
[SOLVED] Contact Form Help danny322 PHP Forum 3 Nov 7th, 2007 16:05


All times are GMT. The time now is 11:13.


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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42