weixin_33695450 2016-07-14 07:19 采纳率: 0%
浏览 7

jQuery空结果

Problem

I tried after submitting the form not redirect to email php. For that I used ajax and its somehow working however it's gives back empty result. I have problem with javascript but thats enough difficult to find for my beginner level. I tested to send email without ajax and it sending normally. But redirecting to empty page email.php

HTML

<form method="POST" id="myForm" data-toggle="validator" action="email.php">
  <h2 class="section-heading">Свяжитесь с нами:</h2>
  <div class="form-group">
    <label for="exampleInputEmail1">Имя:</label>
    <input style="background:none;" id="firstName" name="firtname" class="form-control" placeholder="Имя" required>
    <p id="p1"></p>
  </div>
  <div class="form-group">
    <label for="exampleInputEmail1">Тема:</label>
    <input style="background:none;" id="subjectTheme" name="subject" class="form-control" placeholder="Тема" required>
    <p id="p2"></p>
  </div>   
  <div class="form-group">
    <label for="exampleInputEmail1">Электронная почта:</label>
    <input style="background:none;" type="email" id="email" name="email" class="form-control" placeholder="Электронная почта" required>
    <p class="help-block with-errors"></p>
  </div>
  <div class="form-group">
    <label>Сообщение:</label>
    <textarea style="background:none;" name="message" class="form-control" rows="3"></textarea>
  </div>
  <input type="submit" id="sendButton" class="btn btn-default"/>
</form>

Javascript

$('#myForm').submit(function(e) {
   e.preventDefault();
   $.ajax({
        type: 'POST',
        url: 'email.php'
   });
   $('#myForm')[0].reset();
   alert("Success!");

})

PHP

<?php

$to = 'test@mail.ru'; // Replace with your email

$subject = $_POST['subject']; // Replace with your $subject
$headers = 'From: ' . $_POST['email'];  

$message = 'Имя: ' . $_POST['firtname'] . "
" .
           'Электронная почта: ' . $_POST['email'] . "
" .
           'Тема: ' . $_POST['subject'] . "
" .
           'Сообщение: ' . $_POST['message'];

mail($to, $subject, $message, $headers);

?>
  • 写回答

3条回答 默认 最新

  • YaoRaoLov 2016-07-14 07:23
    关注

    You need to pass the informations through AJAX. And reset your form on AJAX success. You didn't do that now. Should look like this:

    $('#myForm').submit(function(e) {
       e.preventDefault(); 
       var firstname = $('#firstName').val();
       var subject = $('#subjectTheme').val();
       var email = $('#email').val();
       var message = $('#message').val();
       $.ajax({
            type: 'POST',
            url: 'email.php',
            data: {firstname: firstname, subject: subject, email: email, message: message },
            success: function(){
               $('#myForm')[0].reset();
               alert("Success!");
            }
       });
    })
    
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作