Several Form Input Into Mail Body Message Using PHPMailer

A

Anonymous

Guest
Hi, first of all I'm not sure if my topic is in the correct section and I'm kind of new to PHP, more focus in HTML & CSS.

I'm trying to recreate this Schedule an Appointment form like in this site https://coopervision.com.my/fabulous40 (so far trying to focus on the email function first)

And I follow the tutorial from https://www.youtube.com/watch?v=Qw49NP5cgpY&t=626s . But that tutorial only contain one input text that will be insert inside mail body. What if I want to insert several input (select input, radio input & calendar input) inside the mail body too?

So far I tried with select input 1st, but it wont work. May I know what I do wrong?

Code:
<?php
//index.php

$error = '';
$outlet = '';
$name = '';
$email = '';
$subject = '';
$message = '';

function clean_text($string)
{
	$string = trim($string);
	$string = stripslashes($string);
	$string = htmlspecialchars($string);
	return $string;
}

if(isset($_POST["submit"]))
{
	if(empty($_POST["outlet"]))
	{
		$error .= '<p><label class="text-danger">Outlet is required</label></p>';
	}
	else
	{
		$outlet = clean_text($_POST["outlet"]);
	}
	if(empty($_POST["name"]))
	{
		$error .= '<p><label class="text-danger">Please Enter your Name</label></p>';
	}
	else
	{
		$name = clean_text($_POST["name"]);
		if(!preg_match("/^[a-zA-Z ]*$/",$name))
		{
			$error .= '<p><label class="text-danger">Only letters and white space allowed</label></p>';
		}
	}
	if(empty($_POST["email"]))
	{
		$error .= '<p><label class="text-danger">Please Enter your Email</label></p>';
	}
	else
	{
		$email = clean_text($_POST["email"]);
		if(!filter_var($email, FILTER_VALIDATE_EMAIL))
		{
			$error .= '<p><label class="text-danger">Invalid email format</label></p>';
		}
	}
	if(empty($_POST["subject"]))
	{
		$error .= '<p><label class="text-danger">Subject is required</label></p>';
	}
	else
	{
		$subject = clean_text($_POST["subject"]);
	}
	if(empty($_POST["message"]))
	{
		$error .= '<p><label class="text-danger">Message is required</label></p>';
	}
	else
	{
		$message = clean_text($_POST["message"]);
	}
	if($error == '')
	{
		require 'class/class.phpmailer.php';
		$mail = new PHPMailer;
		$mail->IsSMTP();								//Sets Mailer to send message using SMTP
		$mail->Host = xxxxx';		//Sets the SMTP hosts of your Email hosting, this for Godaddy
		$mail->Port = '26';								//Sets the default SMTP server port
		$mail->SMTPAuth = true;							//Sets SMTP authentication. Utilizes the Username and Password variables
		$mail->Username = 'xxxxx';					//Sets SMTP username
		$mail->Password = 'xxxxx';					//Sets SMTP password
		$mail->SMTPSecure = '';							//Sets connection prefix. Options are "", "ssl" or "tls"
		$mail->From = $_POST["email"];					//Sets the From email address for the message
		$mail->FromName = $_POST["name"];				//Sets the From name of the message
		$mail->AddAddress('xxxxx', 'Name');		//Adds a "To" address
		$mail->AddCC($_POST["email"], $_POST["name"]);	//Adds a "Cc" address
		$mail->WordWrap = 50;							//Sets word wrapping on the body of the message to a given number of characters
		$mail->IsHTML(true);			
		$mail->Subject = $_POST["subject"];				//Sets the Subject of the message
		$mail->Body = $_POST["outlet"];					//An HTML or plain text message body	
		$mail->Body = $_POST["message"];				//An HTML or plain text message body
		if($mail->Send())								//Send an Email. Return true on success or false on error
		{
			$error = '<label class="text-success">Thank you for contacting us</label>';
		}
		else
		{
			$error = '<label class="text-danger">There is an Error</label>';
		}
		$outlet = '';
		$name = '';
		$email = '';
		$subject = '';
		$message = '';
	}
}

?>
<!DOCTYPE html>
<html>
	<head>
		<title>Eyes Appointment</title>
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
		<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
	</head>
	<body>
		<br />
		<div class="container">
			<div class="row">
				<div class="col-md-8" style="margin:0 auto; float:none;">
					<h3 align="center">Eyes Appointment</h3>
					<br />
					<?php echo $error; ?>
					<form method="post">
						<div class="form-group">
							<select name="outlet" class="form-control" value="<?php echo $outlet; ?>">
							<option>Outlet 1</option>
							<option>Outlet 2</option>
							<option>Outlet 3</option>
							<option>Outlet 4</option>
							<option>Outlet 5</option>
							</select>
						</div>
						<div class="form-group">
							<label>Enter Name</label>
							<input type="text" name="name" placeholder="Enter Name" class="form-control" value="<?php echo $name; ?>" />
						</div>
						<div class="form-group">
							<label>Enter Email</label>
							<input type="text" name="email" class="form-control" placeholder="Enter Email" value="<?php echo $email; ?>" />
						</div>
						<div class="form-group">
							<label>Enter Subject</label>
							<input type="text" name="subject" class="form-control" placeholder="Enter Subject" value="<?php echo $subject; ?>" />
						</div>
						<div class="form-group">
							<label>Enter Message</label>
							<textarea name="message" class="form-control" placeholder="Enter Message"><?php echo $message; ?></textarea>
						</div>
						<div class="form-group" align="center">
							<input type="submit" name="submit" value="Submit" class="btn btn-info" />
						</div>
					</form>
				</div>
			</div>
		</div>
	</body>
</html>
 
Hi,

You need to concatenate multiple fields together.

In your code the second line here will overwrite the first line:
Code:
		$mail->Body = $_POST["outlet"];					//An HTML or plain text message body	
		$mail->Body = $_POST["message"];

You can add a few characters to concatenate the variables together in different ways:

Code:
		$mail->Body = $_POST["outlet"];					//An HTML or plain text message body	
		$mail->Body .= "<br>". $_POST["message"]; // Will add message to body on a new line.

Hope that helps,
Jon
 
Back
Top