douren7179 2016-11-25 17:00
浏览 27
已采纳

ajax表单已提交但没有回复

so i'm submitting this form from a ajax request and its working fine only thing is that there's no response callback after submission

here's my codes :

//index.html
<form method="POST" id="myForm" action="sendmail.php">
  <input type="text" name="sender_email" placeholder="Email" required="">
  <textarea placeholder="Message" name="message" required=""></textarea>
  <input type="submit" name="send" value="SEND">
</form>
$(document).ready(function() {
  $("#myForm").on('submit', function(event) {
    event.preventDefault();
    var formData = $(this).serialize();
    $.ajax({
      type: 'POST',
      url: 'sendmail.php',
      dataType: "json",
      data: formData,
      success: function(response) {
        alert("Mail sent"); // no callback here 
      },
      error: function(xhr, status, error) {
        console.log(xhr);
      }
    });
  });
});

my php:

<?php
    ini_set('display_errors', 1); 
    ini_set('display_startup_errors', 1); 
    error_reporting(E_ALL);
    if(isset($_POST['message'])){
        $to      = 'support@mydomain.com';
        $subject = $_POST['subject']; 
        $message = $_POST['message']; 
        $headers = "From: ".$_POST['sender_nam‌​e​']." <".$_POST['sender_em‌​ail‌​'].">
"; $headers = "Reply-To: ".$_POST['sender_ema‌​il‌​']."
"; 
        $headers = "Content-type: text/html; charset=iso-8859-1
";
        'X-Mailer: PHP/' . phpversion();
        if(mail($to, $subject, $message, $headers)) {
    echo json_encode(["success" => true]); 
    }  else {echo json_encode(['success'=>false]); 
    }
        exit;
     }
    ?>

anyone can point out whats wrong here ??

  • 写回答

1条回答 默认 最新

  • douhui8454 2016-11-25 22:00
    关注

    You are referring to $_POST['subject'], $_POST['sender_nam‌​e​'], $_POST['sender_nam‌​e​'] in your sendmail.php file but not sending these by your form. That's why php throws warnings and you'r not getting any valid ajax response.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号