Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9200505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:47:01+00:00 2026-06-17T22:47:01+00:00

Trying to set up a contact form. Form submits email and optionally CCs sender.

  • 0

Trying to set up a contact form. Form submits email and optionally CCs sender. If javascript is enabled, the form submits to contact-ajax.php (to replace the form with a thanks message), otherwise it submits to contact.php (to redirect to a thanks page). The fallback form works fine, but every time the ajax form fires it also sends a completely blank email (to both sender and recipient, if CC option is checked). I can’t figure out why this is occurring. Any insight would be appreciated.

Form HTML:

<form method="post" id="email_form" name="email_form" action="contact.php">
    <label for="name">Name:</label><br />
    <input type="text" name="name" id="name" placeholder="John Doe" /><br />
    <label for="email">Email Address:</label><br />
    <input type="text" name="email" id="email" placeholder="johndoe@gmail.com" /><br />
    <label for="subject">Subject </label></td><br />
    <input type="text" name="subject" id="subject" placeholder="Subject" /><br />
    <label for="message">Message:</label></td><br />
    <textarea cols="35" rows="5" name="message" id="message" placeholder="Your Message"></textarea><br />
    <input type="checkbox" name="cc" id="cc" checked value="Yes" /><label for="cc"> Send yourself a copy of this message</label><br />
    <input class="submit pulldown button" name="submit" id="submit" type="submit" value="Send Message" />
</form>

Javascript:

$(document).ready(function() {
$('#email_form #submit').click(function(event) {
    event.preventDefault();
    $.ajaxSetup ({
        cache: false
        });
    $.ajax({
        type: 'POST',
        url: 'contact-ajax.php',
        data: $("#email_form").serializeArray(),
        success: function(msg) {
            if(msg=='Mail sent') {
                alert(msg);
                }
            else {
                alert('Error sending mail. Please try again.');
                }
            },
        error: function(ob,errStr) {
            alert('Error sending mail. Please try again.');
            }
        });
    return false;
    });
});

PHP form called by script (contact-ajax.php):

<?php
if($_SERVER['REQUEST_METHOD'] == "POST") {
    $name = $_POST['name'];
    $visitor_email = $_POST['email'];
    $email_subject = $_POST['subject'];
    $message = $_POST['message'];
    $today = date('r');
    $email_from = "address@domain.tld";
    $to = "address@domain.tld";
    $email_body = "On $today $name wrote:\n"."$message";
    $headers = "From: $email_from \r\n";
    $headers .= "Reply-To: $visitor_email \r\n";
    if (isset($_POST['cc'])) {
        $headers .= "CC: $visitor_email \r\n";
        }
    $headers .= "X-Mailer: PHP/". phpversion();
    mail($to,$email_subject,$email_body,$headers);
    if(mail($to, $subject, $body, $headers)) {
        die('Mail sent');
        }
    else {
        die('Error: Mail failed');
        }
    }
?>

The non-Ajax php script (contact.php) is identical, except that it ends with header('Location: thanks_page.html'); instead of the last if statement.

Blank emails contain no subject or message body, but From, Reply-To, and CC fields are all filled in exactly as they are in the correct (non-empty) message. I’ve been trying to figure out the problem for a few days now but can’t seem to track it down.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:47:02+00:00Added an answer on June 17, 2026 at 10:47 pm

    mail function send mail.. you write mail() 2 times.. Enought to check return value in if

    mail($to,$email_subject,$email_body,$headers);
    if(mail($to, $subject, $body, $headers)) { ......
    

    use only this way:

    if(mail($to, $subject, $body, $headers)) { //mail sent 
    } else { 
    //mail not sent 
    }
    

    UPDATE
    use PHPMailer easy to use and i think better than simple php mail function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to set up a simple PHP contact form for a website and
Here's my problem: I'm trying to set up a simple mobile contact form with
I am trying to set up validation on a simple contact form that is
Hi I'm trying to make a mailer for a contact us form. The email
Im trying set the single table inheritance model type in a form. So i
I am trying to use FormCheck for MooTools to validate a basic contact form
Hi Stackoverflow people, I am trying to write my own contact form in Django,
I'm playing around with MVC3 and trying to create a simple contact form that
I'm trying to make my contact form submission process work equally well whether the
Okay, so I have the form set to my PHP page. I also have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.