PHP: Taking form input and sending emails

Now that we have gone through variables, conditional logic, and functions, let’s look at how you take input from the user.  PHP has two associative arrays for form data: ‘$_GET’ and ‘$_POST’.  ‘$_GET’ shows any get data and ‘$_POST’ shows any post data.

An example of how to take and dump get data:

<!DOCTYPE html>
<html>
<head>
	<title>Contact us!</title>
</head>
<body>
	<form method="get">
		<p>
			<lable for="name">Name: </lable>
			<input name="name" type="text" id="name">
		</p>
		<p>
			<lable for="comment">Comment: </lable>
			<textarea name="comment" id="comment"></textarea>
		</p>
		<p>
			<input type="submit" name="submit" value="submit">
		</p>
	</form>

<?php
		// Is there anything within the $_get array?
		if ($_GET){
			// Was the form method 'Get'? Dump the data.
			echo "Get Data:n";
			print_r($_GET);
		}
	?>
</body>
</html>

An example of how to take and dump post data:

<!DOCTYPE html>
<html>
<head>
	<title>Contact us!</title>
</head>
<body>
	<form method="post">
		<p>
			<lable for="name">Name: </lable>
			<input name="name" type="text" id="name">
		</p>
		<p>
			<lable for="comment">Comment: </lable>
			<textarea name="comment" id="comment"></textarea>
		</p>
		<p>
			<input type="submit" name="submit" value="submit">
		</p>
	</form>

<?php
		// Is there anything within the $_post array?
		if ($_POST){
			// Was the form method 'Post'? Dump the data.
			echo "Post Data:n";
			print_r($_POST);
		}
	?>
</body>
</html>

When print_r() outputs the contents of the array, there will be one item for each form element.  This means that the output should look like …

Post Data: Array ( [name] => test [comment] => test [submit] => submit )

Please keep in mind that the submit button is a form element, so it will be within the array.

 

Now that we have input in our webform, we should do something with it.  Let’s send the data out as an email.  Before we can do that, we will need to figure out how to send an email

<?php
	//Mandatory Attributes
	$to = "[email protected]";
	$subject = "The subject of the email";
	$body = "The body of the email";
	//Optional Attribute
	$headers = "From: [email protected]rn";
	$headers .= "Content-Type: text/plain; charset=utf-8rn";
	$headers .= "Cc: [email protected]";

	//Send the email
	//The last two parameters are optional
	$success = mail($to, $subject, $body, $headers, '[email protected]');
?>

The ‘to’, ‘subject’, and ‘body’ attributes are required.  The additional headers and parameters are optional.  Notice that each thing needs to be contained within it’s own string before being passed into mail().  Also keep in mind that the mail function returns a boolean (success/failure).  you can use that for display of follow-up dialogs.

Let’s put this all together, now

<!DOCTYPE html>
<html>
<head>
	<title>Contact us!</title>
</head>
<body>
	<form method="post">
		<p>
			<lable for="name">Name: </lable>
			<input name="name" type="text" id="name">
		</p>
		<p>
			<lable for="comment">Comment: </lable>
			<textarea name="comment" id="comment"></textarea>
		</p>
		<p>
			<input type="submit" name="submit" value="submit">
		</p>
	</form>

<?php
		// Are 'name' and 'comment' within the $_post array?
		if ($_POST['name'] != '' && $_POST['comment'] != ''){
			// If the name and comment are defined, send the email

			//Mandatory Attributes
			$to = "[email protected]";
			$subject = "Contact Form Submission";
			$body = "Name: {$_POST['name']}nComment: {$_POST['comment']}";

			//Send the email
			$success = mail($to, $subject, $body);

			if($success){
				echo 'Your email has been successfully sent.';
			}else{
				echo 'Error: Your email could not be successfully sent.';
			}

		// Did they click the submit button?
		}elseif($_POST){
			echo 'Error: You must provide a Name and a Comment.';
		}
	?>
</body>
</html>

Notice that we have checked to make sure that ‘name’ and ‘comment’ have values.  We only prompt the user about it if there are elements (the submit button) defined within ‘$_POST’, though.  We then make sure that the email was successfully transmitted to the mail server.  This won’t tell you if the email was successfully sent but the error message should trip if the mail server is down.

Leave a Reply

Your email address will not be published. Required fields are marked *