嘿,伙计们,我已经玩这个 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 时,它会起作用。这里的某个地方有我遗漏的错误吗?任何帮助表示赞赏!