Skip to main content
edited title
Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

How to improve this current PHP e-mail contact form? with AJAX

added 12 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

How to improve thethis current contact form?

I have just started learning whohow to build websites, and I have been experimenting how to createby creating a contact form. 

Here is what I currently have - Cancan anyone suggest and recommend ways enhance my current contact form system?

HTML:HTML:

How to improve the current contact form

I have just started learning who to build websites, and been experimenting how to create a contact form. Here is what I currently have - Can anyone suggest and recommend ways enhance my current contact form system?

HTML:

How to improve this current contact form?

I have just started learning how to build websites, and I have been experimenting by creating a contact form. 

Here is what I currently have - can anyone suggest and recommend ways enhance my current contact form system?

HTML:

Source Link
Leo
  • 789
  • 4
  • 14

How to improve the current contact form

I have just started learning who to build websites, and been experimenting how to create a contact form. Here is what I currently have - Can anyone suggest and recommend ways enhance my current contact form system?

JavaScript:

$("#contactForm").submit(function (event) {

    /* stop form from submitting normally */
    event.preventDefault();

    /* get some values from elements on the page: */
    var $form = $(this),
        $submit = $form.find('button[type="submit"]'),
        name_value = $form.find('input[id="name"]').val(),
        email_value = $form.find('input[id="email"]').val(),
        message_value = $form.find('textarea[id="message"]').val(),
        url = $form.attr('action');

    /* send the data using post */
    var posting = $.post(url, {
        name: name_value,
        email: email_value,
        message: message_value,
        ajax: 1
    });

    posting.done(function (data) {
        $form.find('span.error').remove();

        if (data == "1") {

            /*Change the button text.*/
            $submit.text('Sent. Thank You!');
            $submit.addClass("sent");

        } else {
            $submit.after('<span style="display: inline-block; padding: 20px 5px; color: #bd3d3d" class="error">Failed to send the message, please check your entries and try again.</span>');
            /*Change the button text.*/
            $submit.text('Try Again');
        }
    });
});

PHP

<?php

    if(isset($_POST['name']) && $_POST['email'] && $_POST['message']) {
            
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
     
    $to = "[email protected]";
    $subject = "New Message From: $name";
    $message .= "$messege";
            
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
    $headers .= 'From: '.$email . "\r\n";
    $mailed = ( mail($to, $subject, $message, $headers) );
 
    if( isset($_POST['ajax']))
        $response = ($mailed) ? "1" : "0";
    else
        $response = ($mailed) ? "<h2>Success!</h2>" : "<h2>Error! There was a problem with sending.</h2>";
        echo $response;
        }
    else
        {
        echo "Form data error!";
    }
?>

HTML:

<form id="contactForm" action="email.php" method="post" name="contactForm">
    <div class="name">
        <label>Your Name</label>
        <input id="name" type="text" placeholder="Enter Name" required>
    </div>
    <div class="email">
        <label>Email Address</label>
        <input id="email" type="email" placeholder="Enter Email" required>
    </div>
    <div class="message">
        <label>Message</label>
        <textarea id="message" required></textarea>
    </div>
    <button id="submit" type="submit">Send</button>
</form>