0

Definitely a PHP newbie here. I have a contact form for someone sending an email through the site that I'd like to have up on GoDaddy for a client (using Bootstrap). I've tested it on there. I have the following, but it's not sending the form probably b/c of the error below. My HTML, JS, and PHP are below. I know there's something called PHPMailer that's now used but this should work in any case. The POST happens, but never sends & I don't get a success message. Thanks. This is the error but the PHP validator says it's fine.

Parse error: syntax error, unexpected T_FOREACH in /home/content/87/4993187/html/v2/contact.php on line 16

//My HTML 

 <!--Contact form-->
<form id="contact-form" method="post" action="contact.php" role="form">

      <div class="messages"></div>

      <div class="controls">

          <div class="row">
              <div class="col-md-6">
                  <div class="form-group">
                      <label for="form_name">First Name *</label>
                      <input id="form_name" type="text" name="name" class="form-control" placeholder="Please enter your firstname *" required="required" data-error="Firstname is required.">
                      <div class="help-block with-errors"></div>
                  </div>
              </div>
              <div class="col-md-6">
                  <div class="form-group">
                      <label for="form_lastname">Last Name *</label>
                      <input id="form_lastname" type="text" name="surname" class="form-control" placeholder="Please enter your lastname *" required="required" data-error="Lastname is required.">
                      <div class="help-block with-errors"></div>
                  </div>
              </div>
          </div>
          <div class="row">
              <div class="col-md-6">
                  <div class="form-group">
                      <label for="form_email">Email *</label>
                      <input id="form_email" type="email" name="email" class="form-control" placeholder="Please enter your email *" required="required" data-error="Valid email is required.">
                      <div class="help-block with-errors"></div>
                  </div>
              </div>
              <div class="col-md-6">
                  <div class="form-group">
                      <label for="form_phone">Phone</label>
                      <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>
          <div class="row">
              <div class="col-md-12">
                  <div class="form-group">
                      <label for="form_message">Message *</label>
                      <textarea id="form_message" name="message" class="form-control" placeholder="Message for me *" rows="4" required="required" data-error="Please,leave us a message."></textarea>
                      <div class="help-block with-errors"></div>
                  </div>
              </div>
              <div class="col-md-12">
                  <input type="submit" class="btn btn-success btn-send" value="Send message">
              </div>
          </div>
          <div class="row">
              <div class="col-md-12">
                  <p class="text-muted"><strong>*</strong> These fields are required. </p>
              </div>
          </div>
      </div>

  </form>
</div>

 //My JS 

$(function () {

var $contactForm = $('#contact-form')

$contactForm.validator();

$contactForm.on('submit', function (e) {
    if (!e.isDefaultPrevented()) {
        var url = "contact.php";

        $.ajax({
            type: "POST",
            url: url,
            data: $(this).serialize(),
            success: function (data)
            {
                var messageAlert = 'alert-' + data.type;
                var messageText = data.message;

                var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' + messageText + '</div>';
                if (messageAlert && messageText) {
                    $contactForm.find('.messages').html(alertBox);
                    $contactForm[0].reset();
                }
            }
        });
        return false;
    }
  })
});

//and my contact.php

<?php


$from = 'Contact form <testing@comcast.net>';
$sendTo = 'Contact form <erics@gmail.com';
$subject = 'New inquiry';
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message');
$okMessage = 'Submission successful, Thank you, we will get back to you shortly';
$errorMessage = 'There was an error while submitting the form. Please try again later';


try 
{
     $emailText = "You have a message from the site\nnnnnnnnnnnnnnnn\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";
        }
    }


    mail($sendTo, $subject, $emailText, "From:" . $from);

    $responseArray = array('type' => 'success', 'message'=> $okMessage);
}

catch (\Exception $e)
{
    $responseArray = array('type' => 'danger', 'message' => $errorMessage);
}

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 {
    echo $responseArray['message'];
}
erics15
  • 567
  • 1
  • 7
  • 16

0 Answers0