嗨,我想用html表格php和ajax创建一个表格,但是我有问题。请检查我的代码。提交表格后,我在网络邮件中未定义,请检查代码并帮助我该怎么办?谢谢。
html格式:
<form id="sent" action="contact-sent.php" method="post">
<div class="form-group">
<input type="text" name="username" id="username"">
</div>
<div class="form-group">
<input type="email" name="usermail" id="usermail" placeholder="enter your mail" class="form-control"">
</div>
<div class="form-group ">
<textarea class="form-control" name="usermessage" id="usermessage" maxlength="350" placeholder="enter your message"></textarea>
</div>
<div class="col-lg-12 col-md-12 col-xs-12 col-sm-12">
<input type="submit" name="submit" id="btn_submit" class="btn btn-block btn-warning" value="send" />
</div>
ajax代码:
$(document).ready(function () {
$('#sent').submit(function (e) {
e.preventDefault();
var username = $('username').val();
var usermail = $('usermail').val();
var usermessage = $('usermessage').val();
$.ajax ({
url: 'contact-sent.php',
type: 'POST',
data: 'username=' + username +'&usermail=' + usermail + '&usermessage=' + usermessage,
success: function (data) {
$('#btn_submit').html(data);
}
});
});
});
和php代码:
<?php
@$username = addslashes( $_POST['username'] );
@$usermail = addslashes( $_POST['usermail'] );
@$usermessage = addslashes( $_POST['usermessage'] );
$header = "From: $email\n"
. "Reply-To: $email\n";
$header .= "Content-Type: text/plain; charset=UTF-8\n";
$email_to = "webacc2012@gmail.com";
$message ="first and last name: $username\n"
. "email address: $usermail\n"
. "message: $usermessage\n";
@mail($email_to, $subject ,$message ,$header ) ;
echo('<script>$(\'#contact-modal\').modal(\'show\');</script>');