0

I have a form on my website that has been working fine. A week or two ago it just stopped working. I have gone through it (I am not a JS/PHP expert) and it seems to verify the form but then it never gets sent.

form from the footer

<!--   beginning of form   -->
<form id="contact-form" class="contact__form" method="post" role="form" autocomplete="off" action="https://shamar.org/contact.php">

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

    <div class="controls">

    <!-- form element -->
    <div class="row">
      <input name="firstname" type="text" id="firstname" class="firstname1">
        <div class="col-md-6 form-group">
          <input name="name" type="text" class="form-control" placeholder="Name" required="required" data-error="Name is required.">
          <div class="help-block with-errors"></div>
        </div>
        <div class="col-md-6 form-group">
          <input name="email" type="email" class="form-control" placeholder="Email" required="required" data-error="Valid email is required.">
          <div class="help-block with-errors"></div>
        </div>
        <div class="col-12 form-group">
          <textarea name="message" class="form-control" rows="6" placeholder="Message" rows="6" required="required" data-error="Please, leave a message."></textarea>
          <div class="help-block with-errors"></div>
        </div>
        <div class="col-12">
        <input name="submit" type="submit" class="btn btn-success btn-sm" value="Send Message">
        </div>

    </div>
</form>
<!-- end form element -->

contact.php

<?php
/*
 *  CONFIGURE EVERYTHING HERE
 *  Original script: https://bootstrapious.com/p/how-to-build-a-working-bootstrap-contact-form
 *  See also /js/contact.js
 */

// an email address that will be in the From field of the email.
$from = 'mail@leeunderwood.org';

// an email address that will receive the email with the output of the form
$sendTo = 'mail@leeunderwood.org';

// subject of the email
$subject = 'Shamar Contact Form';

// form field names and their translations.
// array variable name => Text to appear in the email
$fields = array('name' => 'Name', 'email' => 'Email', 'message' => 'Message');

// message that will be displayed when everything is OK :)
$okMessage = 'Your message was sent successfully. 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);
error_reporting(0)

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

contact.js

$(function () {

  // Original script: https://bootstrapious.com/p/how-to-build-a-working-bootstrap-contact-form
  // init the validator
  // validator files are included in the download package
  // otherwise download from http://1000hz.github.io/bootstrap-validator
  // See also: /contact.php

  $('#contact-form').validator();

  // when the form is submitted
  $('#contact-form').on('submit', function (e) {

//    if($('input#hnypt').val().length !=0 {
//        return false
//        }

    // if the validator does not prevent form submit


    if (!e.isDefaultPrevented()) {
        var url = "/contact.php";

        // POST values in the background the script URL
        $.ajax({
            type: "POST",
            url: url,
            data: $(this).serialize(),
            success: function (data)
            {
              // data = JSON object that contact.php returns

              // we receive the type of the message: success x danger and apply it to the
              var messageAlert = 'alert-' + data.type;
              var messageText = data.message;

              // let's compose Bootstrap alert box HTML
              var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' + messageText + '</div>';

              // If we have messageAlert and messageText
              if (messageAlert && messageText) {
              // inject the alert to .messages div in our form
              $('#contact-form').find('.messages').html(alertBox);
              // empty the form
              $('#contact-form')[0].reset();
              }
            }
        });
        return false;
      }
  })
});

I don't really know how to proceed as it was working before. Does anyone have any ideas of what the problem is?

(I had also tried to include a "honeypot" script but could never get it right.)

Thanks.

Lee

Mujahid Bhoraniya
  • 1,518
  • 10
  • 22
Lee
  • 491
  • 4
  • 15
  • `it seems to verify the form but then it never gets sent` - what does this mean? Be specific, describe exactly what you're seeing. Start with some basic debugging. Have you checked the web server and PHP log files? How about mail server logs? Spam folder? Check your browser's devtools, are there any JS errors shown on the console? How about the network tab, can you see the AJAX post happening OK, and if so, how does the server respond? – Don't Panic Mar 05 '20 at 05:50
  • It had nothing to do with the other post. It was a bracket misssing in the following line: try (missing bracket) { So simple. [Just a bit of testing at]( https://phpcodechecker.com/index.htm). – Lee Mar 06 '20 at 00:49

0 Answers0