使用ajax发送信息到submit.php

时间:2013-08-22 23:16:22

标签: php ajax forms email return

嗨,伙计们,我几天来一直在玩这个ajax电话。我之前发布了一个没有变量的电子邮件,但现在我无法接到提交带有变量或没有变量的表单。这是我的代码:

      <form id="contactform" method="post" action="submit.php">

          <input type="text" placeholder="Name" name="username" id="username" />

          <input type="email" placeholder="Your Email Address" name="email" id="email" />

          <input type="button" id="submit" class="submit" value="submit"></a>

      </form>

AJAX:

<script type="text/javascript">
  $("#submit").click(function(event){
   var data = $('#contactform').serialize();
   event.preventDefault();
     $.ajax({
         url: "submit.php",
         type: "POST",
         data: data,
         success: function() {                        
             alert("Success!");        
         }
       }); 
     return false; 
   });
 });
</script>

submit.php:

<?php
    $from = "info@email.com";
    $usersubject = "Thank You!";
    $usermessage = "Thank you for signing up!";
    $to = $_REQUEST['email'];
    $subject = "Form Info";
?>

<?php
    $name = $_REQUEST['username'];
    $email = $_REQUEST['email'];
    $message = "Name: $name
    Email: $email";
    $headers = "From:" . $from;
    //mail($to,$subject,$message,$headers);
    mail($email,$usersubject,$usermessage,$headers);
    echo 'Success';
?>

submit.php在您使用变量直接访问它时起作用(www.website.com/submit.php?username=Dave&email=davesemail@mail.com)。在这里某处我有错误吗?任何帮助表示赞赏!

3 个答案:

答案 0 :(得分:0)

这就是你想要的,但你应该了解Json。 数据= { “姓名”:“James Bond”,“email”:“AGENT007@MI5.gov” }

答案 1 :(得分:0)

如果您不使用文档就绪块:

$( document ).ready(function() {
  // your code here
});

然后确保将你的javascript代码放在关闭body标记()之前。 否则,如果您在没有准备好文档的情况下将javascript放在标题中,则会在页面上出现提交按钮之前执行javascript代码,因此$('#submit')选择器将返回零元素。

答案 2 :(得分:0)

HTML:

     <form  method="post" action="submit.php">

     <input type="text" placeholder="Name" name="username" id="username" />

     <input type="email" placeholder="Your Email Address" name="email" id="email" />

     <input type="button" onClick="submit()" value="submit"></a>

     </form>

AJAX:

     <script type="text/javascript">
  function submit(){
  var user = document.getElementById("username").value;
  var email = document.getElementById("email").value;
     $.ajax({
     url: "submit.php",
     type: "POST",
     data: {uname:user,email:email},
     success: function() {                        
         alert("Success!");        
     }
      }); 
     return false; 
   }
   </script>

PHP:

     <?php
    $from = "info@email.com";
    $usersubject = "Thank You!";
    $usermessage = "Thank you for signing up!";
    $to = $_POST['email'];//one that assigned to data in ajax
    $subject = "Form Info";
      ?>

    <?php
     $name = $_POST['uname']; //one that assigned to data in ajax
     $email = $_POST['email'];//one that assigned to data in ajax
     $message = "Name: $name
     Email: $email";
     $headers = "From:" . $from;
    //mail($to,$subject,$message,$headers);
    mail($email,$usersubject,$usermessage,$headers);
     echo 'Success';
     ?>