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

    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助