douzoudang1511 2013-08-22 23:16
浏览 78

使用ajax发送信息到submit.php

Heys guys, I've been playing around with this ajax call for a few days now. I had it posting an email with no variables earlier, but now I can't get the call to submit the form with a variable or without. Here is my code:

      <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 works when you access it directly with the variable (www.website.com/submit.php?username=Dave&email=davesemail@mail.com). Is there an error somewhere here that I'm missing? Any help is appreciated!

  • 写回答

3条回答 默认 最新

  • dtjpnd7517 2013-08-22 23:40
    关注

    This what you want but you should learn about Json. data={ "name":"James Bond","email":"AGENT007@MI5.gov " }

    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错