0

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!

AP_19
  • 31
  • 6
  • You probably want to pass the contextual information as params. See here for a hint: https://stackoverflow.com/q/871858/1531971 –  Jan 05 '18 at 18:25

1 Answers1

0

You could give to every form a different value via $_GET:

<form class="container" id="contact-form" method="post" action="contact.php?landing_page=1" role="form">

And then depending of $_GET['landing_page'] value set $from, $sendTo and $subject variables:

$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'marketing' => 'Marketing');
        $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';

if (isset($_GET['landing_page']))
{
    if ($_GET['landing_page'] == 1)
    {
        $from = 'Contact form <creative@email.com>';
        $sendTo = 'Contact form <creative@email.com>';
        $subject = 'Contact form enquiry from the Creative Services Landing Page';

    }
    elseif($_GET['landing_page'] == 2)
    {
        $from = 'Contact form <creative@email.com>';
        $sendTo = 'Contact form <creative@email.com>';
        $subject = 'Contact form enquiry from the Creative Services Landing Page';
    }
    elseif($_GET['landing_page'] == 3)
    {
        $from = 'Contact form <creative@email.com>';
        $sendTo = 'Contact form <creative@email.com>';
        $subject = 'Contact form enquiry from the Creative Services Landing Page';
    }
    else
    {
        throw new \Exception('Invalid landing_page value');
    }
}
else
{
    throw new \Exception('Invalid landing_page value');
}


/*
 *  LET'S DO THE SENDING
 */
Lluís Camino
  • 178
  • 3
  • 10
  • Hi Lluís - thanks for the reply and looking at this for me - very much appreciated! I've added your suggestion and am getting an "500 (Internal Server Error)" error from `jquery.js` in my Console when I click Send on the form and hence is not processing at all. `xhr.send( options.hasContent && options.data || null );` on `contact.php`. I'm guessing I need to declare something for it to show up... – AP_19 Jan 05 '18 at 19:13