Web Design Forum: PHP Contact form - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

PHP Contact form Rate Topic: -----

#1 User is offline   lee grant 

  • Smile =]
  • PipPipPipPip
  • Group: Members
  • Posts: 626
  • Joined: 15-July 09
  • Reputation: 22
  • Gender:Male
  • Location:England
  • Experience:Intermediate
  • Area of Expertise:Designer/Coder

Posted 16 March 2010 - 02:16 PM

Hi,

I've made my first attempt at making a php contact form but have come to a bit of a stand still.

I've tried making some forms required but failed miserably and also the email feild to detect if a correct email was entered, but again failed miserably!

If anyone could help that'd be great.
<?php
	if(isset($_POST['submit'])) {
		$to = "lee@klevermedia.co.uk";
		$subject = "Contact form";
		$name = $_POST['name'];
		$email = $_POST['email'];
		$company = $_POST['company'];
		$contact = $_POST['contact'];
		$dropdown = $_POST['dropdown'];
		$message = $_POST['message'];
		$header="from: $name_field <$email>";
		
		$body = "From: $name\n\n E-Mail: $email\n\n Company: $company\n\n Contact number: $contact\n\n Drop down: $dropdown\n\n Message:\n\n $message";
		
		echo "Your email has been sent to $to";
		mail($to, $subject, $body, $header);
	} 
	else {
		header('Location: http://www.klevermedia.co.uk') ;
	}
?>

0

#2 User is offline   JSXSolutions 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 97
  • Joined: 16-March 10
  • Reputation: 2
  • Gender:Male
  • Location:Isle Of Wight, UK
  • Experience:Intermediate
  • Area of Expertise:Coder

Posted 16 March 2010 - 04:44 PM

I would declare a variable $error = 0;
and create an if statement:
if($name_field == "")

                {
			print("Please enter your name<br/>");
			$error += 1;
		}

if($error == 0)
		{
			if(mail($to, $subject, $body, $from_header))
			{
				echo "Data has been submitted successfully";
				
			}
			else
			{
				echo "Failed sending email";
			}
		}
		else
		{
			print("<br/><br/><b>Click <a href=\"javascript: history.go(-1);\">here</a> to go back</b>");
		}
	}
	else
	{
		echo "No data was posted from the contact form";
		print("<br/><br/>Click <a href=\"contact.html\">here</a> to go to the contact form");		
	}


code is partially stolen from another script give me a shout if you have any problems.
0

#3 User is offline   Dino Media 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 37
  • Joined: 02-October 09
  • Reputation: 0
  • Gender:Male
  • Location:Bristol, United Kingdom
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 16 March 2010 - 04:52 PM

View PostJSXSolutions, on 16 March 2010 - 04:44 PM, said:

I would declare a variable $error = 0;
and create an if statement:
if($name_field == "")

                {
			print("Please enter your name<br/>");
			$error += 1;
		}

if($error == 0)
		{
			if(mail($to, $subject, $body, $from_header))
			{
				echo "Data has been submitted successfully";
				
			}
			else
			{
				echo "Failed sending email";
			}
		}
		else
		{
			print




I do a very similar thing...

$error = array();

if(strlen($name_field) == 0)
{
	array_push($error,'Please enter your name')
}

if(count($error) > 0)
{
	// return user to form, where you can print errors in <ul>
}
else
{
	// send the email
}
			

0

#4 User is offline   webdesigner93 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,858
  • Joined: 22-September 09
  • Reputation: 212
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 16 March 2010 - 05:06 PM

View PostDino Media, on 16 March 2010 - 04:52 PM, said:

I do a very similar thing...

$error = array();

if(strlen($name_field) == 0)
{
	array_push($error,'Please enter your name')
}

if(count($error) > 0)
{
	// return user to form, where you can print errors in <ul>
}
else
{
	// send the email
}
			



Or u can do


$errors = array();

if(!$name_field){
$errors[] = "Please enter name!";


}

if(count($errors) > 0){
foreach($errors AS $error){
echo "$error <br />";
}
}else{
//send mail
}


0

#5 User is offline   Dino Media 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 37
  • Joined: 02-October 09
  • Reputation: 0
  • Gender:Male
  • Location:Bristol, United Kingdom
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 16 March 2010 - 05:09 PM

View Postwebdesigner93, on 16 March 2010 - 05:06 PM, said:

Or u can do


$errors = array();

if(!$name_field){
$errors[] = "Please enter name!";


}

if(count($errors) > 0){
foreach($errors AS $error){
echo "$error <br />";
}
}else{
//send mail
}




True. I prefer to set an array, and save it as a session and send back to the form. The error array can then be looped/displayed at the start of the form. This also means if the user presses refresh, the data isn't reposted and the error array is still there.
0

#6 User is offline   Dino Media 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 37
  • Joined: 02-October 09
  • Reputation: 0
  • Gender:Male
  • Location:Bristol, United Kingdom
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 16 March 2010 - 05:09 PM

deleted
0

#7 User is offline   JSXSolutions 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 97
  • Joined: 16-March 10
  • Reputation: 2
  • Gender:Male
  • Location:Isle Of Wight, UK
  • Experience:Intermediate
  • Area of Expertise:Coder

Posted 16 March 2010 - 05:24 PM

For the email verification (to ensure its an email address and not jibberish)
i often use a PHP + Regex function in addition to the earlyer script


if($email_field == "")
		{
			print("Please enter your email<br/>");
			$error += 1;
		}
		else if(!emailValidate($email_field))
		{
			print("Please enter a valid email address");
			$error += 1;
		}

function emailValidate($emailAddress)
	{
		$validEmail = str_replace("\'","'",$emailAddress); //To allow emails with apostrophes in :o)
		$regexString = "^['-.+_a-z0-9]+@(([-.a-z0-9]+\.[a-z]{2,3})|(:?(:?25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(:?25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))$";
		
		if(eregi($regexString,$validEmail))
			return true;
		else
			return false;
	}

0

#8 User is offline   lee grant 

  • Smile =]
  • PipPipPipPip
  • Group: Members
  • Posts: 626
  • Joined: 15-July 09
  • Reputation: 22
  • Gender:Male
  • Location:England
  • Experience:Intermediate
  • Area of Expertise:Designer/Coder

Posted 22 March 2010 - 09:53 PM

Excellent thanks a lot, I'll try this out see how it goes.
0

#9 User is offline   JSXSolutions 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 97
  • Joined: 16-March 10
  • Reputation: 2
  • Gender:Male
  • Location:Isle Of Wight, UK
  • Experience:Intermediate
  • Area of Expertise:Coder

Posted 23 March 2010 - 12:42 PM

not a problem, let us know how you get on :)
0

#10 User is offline   lee grant 

  • Smile =]
  • PipPipPipPip
  • Group: Members
  • Posts: 626
  • Joined: 15-July 09
  • Reputation: 22
  • Gender:Male
  • Location:England
  • Experience:Intermediate
  • Area of Expertise:Designer/Coder

Posted 23 March 2010 - 06:41 PM

I'm going wrong somewhere here, I tried this ..


<?php
        if(isset($_POST['submit'])) {
                $to = "lee@klevermedia.co.uk";
                $subject = "Contact form";
                $name = $_POST['name'];
                $email = $_POST['email'];
                $company = $_POST['company'];
                $contact = $_POST['contact'];
                $dropdown = $_POST['dropdown'];
                $message = $_POST['message'];
                $header="from: $name_field <$email>";
                
                $body = "From: $name\n\n E-Mail: $email\n\n Company: $company\n\n Contact number: $contact\n\n Drop down: $dropdown\n\n Message:\n\n $message";
                
                echo "Your email has been sent to $to";
                mail($to, $subject, $body, $header);
        } 
        else {
                header('Location: http://www.klevermedia.co.uk') ;
        }
		if($name == "")

                {
                        print("Please enter your name<br/>");
                        $error += 1;
                }

		if($error == 0)
                {
                        if(mail($to, $subject, $body, $from_header))
                        {
                                echo "Data has been submitted successfully";
                                
                        }
                        else
                        {
                                echo "Failed sending email";
                        }
                }
                else
                {
                        print("<br/><br/><b>Click <a href=\"javascript: history.go(-1);\">here</a> to go back</b>");
                }
        }
        else
        {
                echo "No data was posted from the contact form";
                print("<br/><br/>Click <a href=\"contact.html\">here</a> to go to the contact form");           
        }
?>


But when I press send with no fields filled in I get sent to /mailform.php

http://klevermedia.co.uk/contactform/ .. incasse nayone wanted to check.

Thanks again
0

#11 User is offline   JSXSolutions 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 97
  • Joined: 16-March 10
  • Reputation: 2
  • Gender:Male
  • Location:Isle Of Wight, UK
  • Experience:Intermediate
  • Area of Expertise:Coder

Posted 23 March 2010 - 07:26 PM

on first look add $error = 0; to your variables at the top.
0

#12 User is offline   lee grant 

  • Smile =]
  • PipPipPipPip
  • Group: Members
  • Posts: 626
  • Joined: 15-July 09
  • Reputation: 22
  • Gender:Male
  • Location:England
  • Experience:Intermediate
  • Area of Expertise:Designer/Coder

Posted 23 March 2010 - 08:30 PM

I've tried that, I seem to still get the same problem ?

I'm quite poor with php! lol
0

#13 User is online   andyl 

  • Web Guru huh...
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,408
  • Joined: 21-January 10
  • Reputation: 191
  • Gender:Male
  • Location:Surrey / HANTS
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 23 March 2010 - 08:51 PM

Try this:

<?php
if(isset($_POST['submit'])) {
    $to = "lee@klevermedia.co.uk";
    $subject = "Contact form";
    $name = $_POST['name'];
    $email = $_POST['email'];
    $company = $_POST['company'];
    $contact = $_POST['contact'];
    $dropdown = $_POST['dropdown'];
    $message = $_POST['message'];
    $header="from: $name <$email>";
    
    $body = "From: $name\n\n E-Mail: $email\n\n Company: $company\n\n Contact number: $contact\n\n Drop down: $dropdown\n\n Message:\n\n $message";
    
    if(empty($name))
    {
        print("Please enter your name<br/>");
        $error += 1;
    }
    if(empty($email))
    {
        print("Please enter your email<br/>");
        $error += 1;
    }
    if(empty($contact))
    {
        print("Please enter your preferred contact<br/>");
        $error += 1;
    }
    if(empty($message) || (strlen($message) < 25))
    {
        print("Please enter a message with a good level of detail<br/>");
        $error += 1;
    }
    if($error == 0)
    {
        if(mail($to, $subject, $body, $from_header))
        {
                echo "Data has been submitted successfully";
                
        }
        else
        {
                echo "Failed sending email. Please go <a href=\"javascript: history.go(-1);\">back</a> and try again.";
        }
    }
    else
    {
            print("<br/><br/><b>Click <a href=\"javascript: history.go(-1);\">here</a> to go back</b>");
    }
}
else {
    header('Location: http://www.klevermedia.co.uk');
    exit;
}
?>

0

#14 User is offline   lee grant 

  • Smile =]
  • PipPipPipPip
  • Group: Members
  • Posts: 626
  • Joined: 15-July 09
  • Reputation: 22
  • Gender:Male
  • Location:England
  • Experience:Intermediate
  • Area of Expertise:Designer/Coder

Posted 23 March 2010 - 09:21 PM

Thanks for the help Andy, all seems to work great except the message, it still says no message when I put one in the textfield.

Also, if I wanted to look at say a thank you message appearing without going to a new page and the error message showing in real time would I need to look into ajax for this?

EDIT

--

My fault, I wasn't paying enough attention in the code, I just reduced the < 25 to 5 all works, thanks again for your help. Any advice on my 2nd question?

Cheers
0

#15 User is online   andyl 

  • Web Guru huh...
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,408
  • Joined: 21-January 10
  • Reputation: 191
  • Gender:Male
  • Location:Surrey / HANTS
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 23 March 2010 - 10:56 PM

Yup, AJAX would be the right way to go. I'd suggest using jQuery with PHP.
Let me know if you'd like me to implement this for you.
0

#16 User is offline   lee grant 

  • Smile =]
  • PipPipPipPip
  • Group: Members
  • Posts: 626
  • Joined: 15-July 09
  • Reputation: 22
  • Gender:Male
  • Location:England
  • Experience:Intermediate
  • Area of Expertise:Designer/Coder

Posted 23 March 2010 - 10:59 PM

Thanks again, I'm gonna try this or this and see what happens.

No doubt I'll need help lol.
0

#17 User is offline   lee grant 

  • Smile =]
  • PipPipPipPip
  • Group: Members
  • Posts: 626
  • Joined: 15-July 09
  • Reputation: 22
  • Gender:Male
  • Location:England
  • Experience:Intermediate
  • Area of Expertise:Designer/Coder

Posted 01 April 2010 - 07:27 PM

All this javascript lark has me confused ??

I've found a nice little piece I'd like to use here. I've looked at the source code and it's lot different to mine, does anyone have any pointer on implementing it with my html I already have?

Thanks
0

#18 User is offline   Jay Gilford 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,074
  • Joined: 11-October 09
  • Reputation: 179
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 01 April 2010 - 08:56 PM

I am a little worried that through all the replies nobody has mentioned the two words which should be leaping out at you from this script


EMAIL INJECTION!

You should ALWAYS sanitize any data you are given, regardless of how small your site is, how insignificant the code is to the site itself, and how much you trust the user in the first place. Spammers can have a field day with your form if you do this. You could also lose your hosting if it happens too
0

#19 User is offline   lee grant 

  • Smile =]
  • PipPipPipPip
  • Group: Members
  • Posts: 626
  • Joined: 15-July 09
  • Reputation: 22
  • Gender:Male
  • Location:England
  • Experience:Intermediate
  • Area of Expertise:Designer/Coder

Posted 01 April 2010 - 10:25 PM

Thanks for that Jay, will a captcha or the old (are you human? 4+8 = ) help prevent this and make my form safer?
0

#20 User is offline   Jay Gilford 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,074
  • Joined: 11-October 09
  • Reputation: 179
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 01 April 2010 - 11:11 PM

Absolutely not lol. You need to verify that there are things like only one @ in an email address (I advise using filter_var with the FILTER_VALIDATE_EMAIL filter type
http://www.google.co...l=&oq=&gs_rfai=
That should give you some help
0

#21 User is offline   rallport 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 3,495
  • Joined: 03-January 10
  • Reputation: 247
  • Gender:Male
  • Location:England, UK
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 02 April 2010 - 01:11 AM

View Postlee grant, on 23 March 2010 - 09:21 PM, said:

Thanks for the help Andy, all seems to work great except the message, it still says no message when I put one in the textfield.

Also, if I wanted to look at say a thank you message appearing without going to a new page and the error message showing in real time would I need to look into ajax for this?

EDIT

--

My fault, I wasn't paying enough attention in the code, I just reduced the < 25 to 5 all works, thanks again for your help. Any advice on my 2nd question?

Cheers


Just set the form to post to the current page and outout the error/siccess messages in situ :)
0

#22 User is offline   Jay Gilford 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,074
  • Joined: 11-October 09
  • Reputation: 179
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 02 April 2010 - 10:09 AM

If you want it to work without changing page or leaving it whatsoever then yes AJAX is the way to go. The method rallport suggested is fine but will require the page to go back on itself
0

#23 User is offline   lee grant 

  • Smile =]
  • PipPipPipPip
  • Group: Members
  • Posts: 626
  • Joined: 15-July 09
  • Reputation: 22
  • Gender:Male
  • Location:England
  • Experience:Intermediate
  • Area of Expertise:Designer/Coder

Posted 05 May 2010 - 07:02 PM

I've been quite busy so this little task got pushed back, I took up your advice here Jay and tried using a little code I found on google to sanitize the code (email field) but it doesn't seem to stop £$%£$ characters sending the email ..

<?php
if(isset($_POST['submit'])) {
    $to = "lee@klevermedia.co.uk";
    $subject = "Contact form";
    $name = $_POST['name'];
    $email = $_POST['email'];
    $company = $_POST['company'];
    $contact = $_POST['contact'];
    $dropdown = $_POST['dropdown'];
    $message = $_POST['message'];
    $header="from: $name <$email>";
    
    $body = "From: $name\n\n E-Mail: $email\n\n Company: $company\n\n Contact number: $contact\n\n Drop down: $dropdown\n\n Message:\n\n $message";
	
	if ( preg_match( "/[\r\n]/", $email ))
	{
		print("Email incorrect<br/>");
        $error += 1;
	}
    
    if(empty($name))
    {
        print("Please enter your name<br/>");
        $error += 1;
    }
    if(empty($email))
    {
        print("Please enter your email<br/>");
        $error += 1;
    }
    if(empty($contact))
    {
        print("Please enter your preferred contact<br/>");
        $error += 1;
    }
    if(empty($message) || (strlen($message) < 5))
    {
        print("Please enter a message with a good level of detail<br/>");
        $error += 1;
    }
    if($error == 0)
    {
        if(mail($to, $subject, $body, $from_header))
        {
                echo "Data has been submitted successfully";
                
        }
        else
        {
                echo "Failed sending email. Please go <a href=\"javascript: history.go(-1);\">back</a> and try again.";
        }
    }
    else
    {
            print("<br/><br/><b>Click <a href=\"javascript: history.go(-1);\">here</a> to go back</b>");
    }
}
else {
    header('Location: http://www.klevermedia.co.uk');
    exit;
}
?>

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users