Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery + PHP is sending duplicate blank emails
    primarykey
    data
    text
    <p>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.</p> <p>Form HTML:</p> <pre><code>&lt;form method="post" id="email_form" name="email_form" action="contact.php"&gt; &lt;label for="name"&gt;Name:&lt;/label&gt;&lt;br /&gt; &lt;input type="text" name="name" id="name" placeholder="John Doe" /&gt;&lt;br /&gt; &lt;label for="email"&gt;Email Address:&lt;/label&gt;&lt;br /&gt; &lt;input type="text" name="email" id="email" placeholder="johndoe@gmail.com" /&gt;&lt;br /&gt; &lt;label for="subject"&gt;Subject &lt;/label&gt;&lt;/td&gt;&lt;br /&gt; &lt;input type="text" name="subject" id="subject" placeholder="Subject" /&gt;&lt;br /&gt; &lt;label for="message"&gt;Message:&lt;/label&gt;&lt;/td&gt;&lt;br /&gt; &lt;textarea cols="35" rows="5" name="message" id="message" placeholder="Your Message"&gt;&lt;/textarea&gt;&lt;br /&gt; &lt;input type="checkbox" name="cc" id="cc" checked value="Yes" /&gt;&lt;label for="cc"&gt; Send yourself a copy of this message&lt;/label&gt;&lt;br /&gt; &lt;input class="submit pulldown button" name="submit" id="submit" type="submit" value="Send Message" /&gt; &lt;/form&gt; </code></pre> <p>Javascript:</p> <pre><code>$(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; }); }); </code></pre> <p>PHP form called by script (contact-ajax.php):</p> <pre><code>&lt;?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'); } } ?&gt; </code></pre> <p>The non-Ajax php script (contact.php) is identical, except that it ends with <code>header('Location: thanks_page.html');</code> instead of the last <code>if</code> statement.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload