doujiaohuo1096 2013-08-06 18: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-06 18: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 代写uni代码,app唤醒
  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下:
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部