doujiaohuo1096
2013-08-07 02:02
浏览 59

使用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!

图片转代码服务由CSDN问答提供 功能建议

我希望使用jquery / php为跟踪号提交单个表单输入,以便页面不会 刷新。 我能够提交表单并将电子邮件发送给我,但无法弄清楚要放在$ email_body中的内容,所以它实际上会通过电子邮件向我发送表单信息。 电子邮件将发送空白(因此我假设电子邮件在PHP代码中)。 请参阅下面的HTML / JS / PHP:

HTML:

 &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”占位符 =“跟踪代码”value =“”type =“text”/&gt; 
&lt;输入类=“btn btn-default btn-track”name =“submit”type =“submit”value =“发送”/&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;您必须输入跟踪代码!&lt; / label&gt;  
&lt; / form&gt; 
   
 
 

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); /  / ===显示成功消息== 
},
}); 
 
e.preventDefault(); 
 
}}; 
}); 
   \  n 
 

PHP(问题出在哪里):

 &lt;?php 
 
 $ track = $ _POST ['tracking']; 
 \  n $ email_from ='info@website.com'; 
 $ email_subject =“新跟踪信息”; 
 $ email_body =“跟踪号码:$ track”。
 
 
 $ n =“email @ gmail。  com“; 
 $ headers =”From:$ email_from 
 
“; 
 
 //发送电子邮件!
mail($ to,$ email_subject,$ email_body,$ headers); 
 
?  &GT; 
    
 
 

如果我保留$ email_body,我会收到“500 Server Error”。 如果我留空,我会收到一封电子邮件,其中只有“email@gmail.com”,没有跟踪号。

提前感谢您的帮助! < / DIV>

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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

    点赞 打赏 评论
  • doumu5934 2013-08-07 02:15

    your don't have a semicolon in the end

    $email_body = "Tracking Number: $track".
    
    点赞 打赏 评论

相关推荐 更多相似问题