5 Replies - 1427 Views - Last Post: 03 July 2011 - 09:32 AM Rate Topic: -----

#1 D.Mulroy   User is offline

  • D.I.C Regular
  • member icon

Reputation: 81
  • View blog
  • Posts: 430
  • Joined: 30-June 10

Help with php form

Posted 30 June 2011 - 12:07 PM

I am currently making an email form for a site and need a bit of help with my script. I am a beginner with php and are trying to have it so when a user submits the form it displays if the email was sent below the form. I am currently on a computer with out a web server running so I have no way of testing this script. Here is my contact.php page

<!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/xhtml">
<head>
	<link rel="stylesheet" href="styles/reset.css" type="text/css" />
	<link rel="stylesheet" href="styles/960.css" type="text/css" />
	<link rel="stylesheet" href="styles/gallery.css" type="text/css" />
	<link rel="stylesheet" href="styles/style.css" type="text/css" />
	<link rel="stylesheet" href="styles/form.css" type="text/css" />
	<script type="text/javascript" src="js/jquery-1.6.1.min.js"></script>
    <script type="text/javascript">
	var d = new Date();
	</script>
	<title>LT Floral Design</title>
</head>
<body>
	<?php include("email.php"); ?>
	<div id="container" class="container_12">
    	<div id="logo" class="grid_6">
        </div>
        <div id="nav" class="grid_6"><a href="index.html">Home</a><a href="floraldesign.html">Floral Design</a>
        <a href="partplanning.html">Party Planning</a><a href="about.html">About Us</a><a href="contact.php">Contact</a>
        </div>
        <div class="clear"></div>
		<div id="header" class="grid_12">
		</div>
		<div class="clear"></div>
		<div id="formContainer" class="grid_6 prefix_3">
			<h1>Interested in our services?</h1>
			<h2>Fill out the form below!</h2>
			<form method="post" enctype="multipart/form-data" action="<?=$_SERVER['PHP_SELF'] ?>">
				<fieldset>
					<legend>Contact Us</legend>
					<label for="firstName">First Name:</label><input type="text" name="firstName" /><br />
					<label for="lastName">Last Name:</label><input type="text" name="lastName" /><br />
					<label for="email">E-Mail:</label><input type="text" name="email" /><br />
					<label for="subject">Subject:</label>
					<select name="subject">
						<option value="default" selected="selected" disabled="disabled">Please select a subject...</option>
						<option value="Floral Design">Floral Design</option>
						<option value="Party Planning">Party Planning</option>
						<option value="Site">Problem(s) with the site</option>
						<option value="Other">Other</option>	
					</select>
					<br /><br />
					<label for="msg" class="msg">Message:</label><br />
					<textarea cols="50" rows="10" name="msg"></textarea><br />
					<button type="submit">Submit</button>
					<?php echo($resp); ?>
				</fieldset>
			</form>
		</div>  
        <div id="footer" class="grid_12">
        	<div id="createdBy" class="grid_2 suffix_3 alpha">
            	<p>created by Dillon Mulroy</p>
            </div>
            <div id="copyright" class="grid_2 suffix_2">
            	<p>&copy;LT Floral Designs <script type="text/javascript">document.write(d.getUTCFullYear());</script></p>
            </div>
            <div id="contact" class="grid_2 push_1">
            	<p>Contact Us:<br/>[email protected]<br/>717.781.6206</p>
            </div>
        </div>      
	</div>
</body>
</html>



here is the email.php script

<?php
	$outPut;
	function response($resp) 
	{
		/*
		1 = E-Mail Sent
		2 = Problem sending the Email
		3 = Invalid Email
		4 = Validation Issue
		5 = Unfinished form
		*/
		if ($resp == 1)
		{
			$outPut = '<h2>E-Mail sent succesfully!</h2>';
		}
		else if($resp == 2)
		{
			$outPut = '<h2>There was an error sending the E-Mail.</h2>';
		}
		else if($resp == 3)
		{
			$outPut = '<h2>The E-Mail Address is invalid.</h2>';
		}
		else if($resp == 4)
		{
			$outPut =  '<h2>There was a validation error'</h2>;
		}
		else if($resp == 5)
		{
			$outPut =  '<h2>Not all fields were filled out corrctly'</h2>;
		}
	}
	if(isset($_POST['firstName'], $_POST['lastName'], $_POST['email'], $_POST['subject'], $_POST['msg']) && $_POST['subject'] != 'defualt')
	{
		if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false)
			response(3);	
		if(empty($_POST['firstName']) || empty($_POST['lastName']) || empty($_POST['subject']) || empty($_POST['msg']))
			response(5);
		$firstName = filter_var($_POST['firstName'], FILTER_SANITIZE_STRING);
		$lastName = filter_var($_POST['lastName'], FILTER_SANITIZE_STRING);
		$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
		$subject = filter_var($_POST['subject'], FILTER_SANITIZE_STRING);
		$msg = filter_var($_POST['msg'], FILTER_SANITIZE_STRING);
		$fullName = $firstName . ' ' . $lastName;
		if($subject != 'Site')
			$to = '[email protected]';
		else
			$to = '[email protected]';
		$subject .= ' - ' . $lastName;
		$header = 'From: ' . $email;	
		if(mail($to, $subject, $msg, $headers))
			response(1);
			
		else
			response(2);
	}
	else
	{
		response(4);
	}
?>



Am I doing this right? Thanks and sorry for my low level question.

edit: just caught the unclosed h2 tags :x

This post has been edited by D.Mulroy: 30 June 2011 - 12:14 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Help with php form

#2 codeprada   User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: Help with php form

Posted 30 June 2011 - 12:19 PM

Line # 50 & 51 $header != $headers

Consider reformatting your headers a bit more. Your From field should be the server and not an actual email.
[email protected]
Why? Some mail servers verify the From field and the source IP address for a match before marking it as legit or spam.

You'll want something like
$headers = 'From: [email protected]' . "\r\n";
$headers .= 'Reply-To: ' . $email . "\r\n";
Even with this header when you hit the reply to button in your email you'll get $email as the return email.

Also verify that the user inputs pass the validation before using them. If they fail FALSE is returned.

php.net said:

Returns the filtered data, or FALSE if the filter fails.

Was This Post Helpful? 0
  • +
  • -

#3 D.Mulroy   User is offline

  • D.I.C Regular
  • member icon

Reputation: 81
  • View blog
  • Posts: 430
  • Joined: 30-June 10

Re: Help with php form

Posted 30 June 2011 - 12:26 PM

Alright I'll work on changing that, but will action="<?=$_SERVER['PHP_SELF'] ?>", <?php echo($resp); ?>, and <?php include("email.php"); ?> work how i'd like it too?
Was This Post Helpful? 0
  • +
  • -

#4 codeprada   User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: Help with php form

Posted 30 June 2011 - 12:57 PM

Always open PHP tags properly because you can't be sure if short tags are enabled. Sometimes when you're dealing with XML PHP will confuse
<?xml version.....
with PHP if the short tags are enabled so more time than not they aren't.

Don't include your email.php script in that form but rather do a redirect when you've finished sending the mail.
Instead of...
<html>
<?php include ('email.php'); ?>
<form...>
...
</form>
</html>


Do
<form action="email.php" ...

<?php
session_start();
//your code
$_SESSION['result'] = $outPut; //so we can display result on another page
//can be store in a cookie also

//redirect user
header('location: some_page.php');
exit;


This post has been edited by codeprada: 30 June 2011 - 12:58 PM

Was This Post Helpful? 2
  • +
  • -

#5 D.Mulroy   User is offline

  • D.I.C Regular
  • member icon

Reputation: 81
  • View blog
  • Posts: 430
  • Joined: 30-June 10

Re: Help with php form

Posted 30 June 2011 - 01:02 PM

Thanks man I appreciate your time. Very helpful.
Was This Post Helpful? 0
  • +
  • -

#6 D.Mulroy   User is offline

  • D.I.C Regular
  • member icon

Reputation: 81
  • View blog
  • Posts: 430
  • Joined: 30-June 10

Re: Help with php form

Posted 03 July 2011 - 09:32 AM

editx2 fixed all problems :P

This post has been edited by D.Mulroy: 03 July 2011 - 09:54 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1