Note that there are some explanatory texts on larger screens.

plurals
  1. POsubmit form with jquery / ajax / php without page refresh. Can't get form value to email
    text
    copied!<p>I'm looking to submit a single form input for a tracking number using jquery/php so that the page doesn't refresh. I am able to submit the form and have the email sent to me but can't figure out what to put in $email_body so it actually emails me the form info. The email will send blank (So I assume email is in the PHP code). Please see below for HTML/JS/PHP:</p> <p>HTML:</p> <pre><code> &lt;form id="track-form" accept-charset="UTF-8" action="" size="30" class="form-inline" method="POST"&gt; &lt;input class="focus" name="tracking" id="tracking" placeholder="tracking code" value="" type="text"/&gt; &lt;input class="btn btn-default btn-track" name="submit" type="submit" value="Send" /&gt; &lt;img id="success" style="display:none" src="img/check.png" /&gt; &lt;br&gt; &lt;label class="error" for="tracking" id="track-error"&gt;You must enter your tracking code!&lt;/label&gt; &lt;/form&gt; </code></pre> <p>JS:</p> <pre><code>$(document).ready(function() { $('.error').hide(); $("#track-form").on('submit', function(e) { $('.error').hide(); var track = $("input#tracking").val(); if (track == "") { $("label#track-error").show(); $("input#tracking").focus(); return false; }; $.ajax({ url:'sendtrack.php', data:$(this).serialize(), type:'POST', success:function(data){ console.log(data); $("#success").fadeIn(300); //=== Show Success Message== }, }); e.preventDefault(); }); }); </code></pre> <p>PHP (WHERE THE PROBLEM IS):</p> <pre><code>&lt;?php $track = $_POST['tracking']; $email_from = 'info@website.com'; $email_subject = "New Tracking Info"; $email_body = "Tracking Number: $track". $to = "email@gmail.com"; $headers = "From: $email_from \r\n"; //Send the email! mail($to,$email_subject,$email_body,$headers); ?&gt; </code></pre> <p>If I leave $email_body as is I get a "500 Server Error". If I leave it blank I get an email with just "email@gmail.com" in it, no tracking number.</p> <p>Thanks in advance for your help!</p>
 

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