在GoDaddy上使用bootstrap,JS和PHP实现联系表单

时间:2018-04-27 20:13:46

标签: php jquery ajax twitter-bootstrap

这里绝对是一个PHP新手。我有一个联系表格,有人通过网站发送电子邮件,我想在GoDaddy上为客户端(使用Bootstrap)。我在那里测试过它。我有以下内容,但它不是发送以下错误的b / c形式。我的HTML,JS和PHP如下。我知道现在使用的是PHPMailer,但无论如何都应该有效。 POST发生了,但从未发送过&我没有得到成功的消息。谢谢。这是错误,但PHP验证器说没关系。

解析错误:语法错误,第16行/home/content/87/4993187/html/v2/contact.php中的意外T_FOREACH

//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'];
}

0 个答案:

没有答案