Send mail form with AJAX (JavaScript & PHP) doesn't work

I’m new to Stack Overflow.
I’m trying to have a simple e-mail form on my website, so I’ve copied one but can’t get it to work. The "Send" button just doesn’t seem to do anything.
I am not a programmer and know very little about code

A friend told me it’s as if the PHP isn’t recognized.

HTML code

<form id="contact-form" action="" method="POST" class="tm-contact-form">
    <div class="form-group">
        <input type="text" name="name" class="form-control rounded-0" placeholder="Full Name" required />
    <div class="form-group">
        <input type="email" name="email" class="form-control rounded-0" placeholder="Email" required />
    <div class="form-group">
        <select class="form-control" id="contact-select" name="inquiry">
            <option value="-">Subject</option>
            <option value="3DPrints">3D Prints</option>
            <option value="AmpsEffects">Amps &amp; Effect Pedals</option>
            <option value="Other">Other</option>
    <div class="form-group">
        <textarea rows="8" style="resize: none;" name="message" class="form-control rounded-0" placeholder="Message" required></textarea>
    <div class="form-group mb-4">
        <div id="result-message" class="alert alert-success d-none">Send result message will display here</div>
    <div class="form-group tm-text-right">
        <button type="submit" class="tm-btn tm-btn-primary">Send</button>

JS file

$(function () {    
    $("#contact-form").submit(function (e) {
        var form_data = $(this).serialize(); 
            type: "POST", 
            url: "js/call-form.php",
            dataType: "text/json", // Add datatype
            data: form_data
        }).done(function (data) {
            alert("It's OK!");
        }).fail(function (data) {
            alert("It's not OK!");

PHP file

if ((isset($_POST['name'])) && (isset($_POST['email'])) && (isset($_POST['inquiry'])) && (isset($_POST['message']))) { 
    $to = '';
    $subject = '.$_POST['inquiry'].';
    $message = '<html>
                <p><b>From:</b> '.$_POST['name'].'</p>
                <p><b>Email:</b> '.$_POST['email'].'</p><br>
  $headers  = "Content-type: text/html; charset=utf-8 \r\n"; 
/* $headers .= "From:\r\n"; */
mail($to, $subject, $message, $headers);

  echo json_encode(array('status' => 'success'));
} else {
  echo json_encode(array('status' => 'error'));

I have put the PHP file with 755 permissions.

Please help!

80 thoughts on “Send mail form with AJAX (JavaScript & PHP) doesn't work”

Leave a Comment