0

i have a html form and a php file. I want send the form input to my email. I fill and send the form but i have no action. Can you help me please. I don't understand why the form not send. I have included the code below and also a picture of browser console error. I've been trying all day but it doesn't really work. I also had simpler versions of php send files but this didn't work at all

HTML:

<form id="contact-form" method="post" action="contact.php">
   <div class="messages"></div>
   <div class="controls">
      <div class="form-group">
         <input id="form_name" type="text" name="name" placeholder="Name" required="required">
      </div>
      <div class="form-group">
         <input id="form_email" type="email" name="email" placeholder="Email" required="required">
      </div>
      <div class="form-group">
         <textarea id="form_message" name="message" placeholder="Message" rows="4" required="required"></textarea>
      </div>
         <button type="submit" class="btn-curve btn-lit"><span>Send Message</span></button>
    </div>
</form>

PHP:

<?php

$from = 'Contact form';
$sendTo = 'info@example.com'; 
$subject = 'New message from contact form';
$fields = array('name' => 'Name', 'subject' => 'Subject', 'email' => 'Email', 'message' => 'Message');
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again later';

try
{
    $emailText = "You have new message from contact form\n=============================\n";

    foreach ($_POST as $key => $value) {

        if (isset($fields[$key])) {
            $emailText .= "$fields[$key]: $value\n";
        }
    }

    $headers = array('Content-Type: text/plain; charset="UTF-8";',
        'From: ' . $from,
        'Reply-To: ' . $from,
        'Return-Path: ' . $from,
    );
    
    mail($sendTo, $subject, $emailText, implode("\n", $headers));

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

JS:

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

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

            $.ajax({     //at this point is the error
                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) {
                        $('#contact-form').find('.messages').html(alertBox);
                        $('#contact-form')[0].reset();
                    }
                }
            });
            return false;
        }
    });

i get this error from browser:

enter image description here

Swipi
  • 15
  • 3

0 Answers0