0

嘿,伙计们,我已经玩这个 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>

阿贾克斯:

<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>

提交.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';
?>

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

4

3 回答 3

0

这是你想要的,但你应该了解 Json。数据={“姓名”:“詹姆斯邦德”,“电子邮件”:“AGENT007@MI5.gov”}

于 2013-08-22T23:40:51.720 回答
0

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

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

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

于 2013-08-22T23:47:17.577 回答
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>

阿贾克斯:

     <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';
     ?>
于 2013-08-23T01:14:58.103 回答