I was wondering someone could help me out with a best practice to achieve the following.
Please bare in mind I have very little experience with PHP, and am doing something more of a favour for a friend's company (his current developer is travelling and not due back until March).
He's put together 3 basic landing pages (each relevant to an industry), each has the exact same form on. The problem at the moment is the way they work is they all post to the same email address and have the same subject line (they all post to contact.php
).
So what I'd like to do is add an IF statement, and perhaps put output a different $subject
and $sendTo
so they can be unique depending on each form. I've been having a look around and don't really want to have to create a new form and implement that, hopefully, I can just adjust what's here.
Here's contact.php
<?php
// an email address that will be in the From field of the email.
$from = 'Contact form <creative@email.com>';
// an email address that will receive the email with the output of the form
$sendTo = 'Contact form <creative@email.com>';
// subject of the email
$subject = 'Contact form enquiry from the Creative Services Landing Page';
// form field names and their translations.
// array variable name => Text to appear in the email
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'marketing' => 'Marketing');
// message that will be displayed when everything is OK :)
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
// If something goes wrong, we will display this message.
$errorMessage = 'There was an error while submitting the form. Please try again later';
/*
* LET'S DO THE SENDING
*/
// if you are not debugging and don't need error reporting, turn this off by error_reporting(0);
error_reporting(E_ALL & ~E_NOTICE);
try
{
if(count($_POST) == 0) throw new \Exception('Form is empty');
$emailText = "You have a new message from your contact form\n=============================\n";
foreach ($_POST as $key => $value) {
// If the field exists in the $fields array, include it in the email
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
// All the neccessary headers for the email.
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
// Send email
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
// if requested by AJAX request return JSON response
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
// else just display the message
else {
echo $responseArray['message'];
}
And here's the HTML
<form class="container" id="contact-form" method="post" action="contact.php" role="form">
<div class="row justify-content-md-center">
<div class="col-12 col-md-5 col-lg-4">
<div class="messages"></div>
</div>
<div class="col-12 col-md-5 col-lg-4">
<div class="form-group">
<input id="form_name" type="text" name="name" class="form-control" placeholder="Please enter your firstname *" required="required" data-error="Your first name is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-12 col-md-5 col-lg-4">
<div class="form-group">
<input id="form_lastname" type="text" name="surname" class="form-control" placeholder="Please enter your lastname *" required="required" data-error="Your surname is required.">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row justify-content-md-center">
<div class="col-12 col-md-5 col-lg-4">
<div class="form-group">
<input id="form_phone" type="tel" name="phone" class="form-control" placeholder="Please enter your phone">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-12 col-md-5 col-lg-4">
<div class="form-group">
<input id="form_email" type="email" name="email" class="form-control" placeholder="Please enter your email *" required="required" data-error="A valid email is required.">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row justify-content-md-center">
<div class="col-12 col-md-3 col-lg-2">
<button type="submit" class="btn btn--primary">Send</button>
</div>
</div>
</form>
Any help would be hugely appreciated!