doujiaohuo1096 2013-08-07 02:02
浏览 60
已采纳

使用jquery / ajax / php提交表单,无需刷新页面。 无法获得电子邮件的表单价值

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:

HTML:

    <form id="track-form" accept-charset="UTF-8" action="" size="30" class="form-inline" method="POST">
      <input class="focus" name="tracking" id="tracking" placeholder="tracking code" value="" type="text"/>
      <input class="btn btn-default btn-track" name="submit" type="submit" value="Send" />  
      <img id="success" style="display:none" src="img/check.png" />
      <br>    
      <label class="error" for="tracking" id="track-error">You must enter your tracking code!</label>  
    </form>

JS:

$(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(); 

 });
});

PHP (WHERE THE PROBLEM IS):

<?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 
";

//Send the email!
mail($to,$email_subject,$email_body,$headers);

?>

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.

Thanks in advance for your help!

  • 写回答

2条回答 默认 最新

  • dongzouh51192 2013-08-07 02:35
    关注

    Change this line:

    $email_body = "Tracking Number: $track".
    

    to:

    $email_body = "Tracking Number: $track";
    

    The closing semi-colon was missing. You had a dot in its place.

    You can also try:

    $email_body = "Tracking Number: " . $track . "
    ";
    

    NOTE: Using/adding headers as below, will reduce the chance of being mistaken for SPAM.

    For example:

    $headers = 'From: webmaster@example.com' . "
    " .
    'Reply-To: webmaster@example.com' . "
    " .
    'X-Mailer: PHP/' . phpversion();
    

    Therefore your new headers would read as:

    $headers = 'From: $email_from' . "
    " .
    'Reply-To: webmaster@example.com' . "
    " .
    'X-Mailer: PHP/' . phpversion();
    

    As per the mail() function on PHP.net http://php.net/manual/en/function.mail.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?