无论用户数据是否有效,我都会显示一条成功消息。我有一条错误消息,但无法显示它。我不是最好的编码员,所以要友善:)
另外,由于某种原因,当我通过表单收到电子邮件时,用户的电子邮件会显示为 user@MISSING_DOMAIN。我的第一个问题优先,所以如果它太多,你不必帮助我。
表格代码
<form action="contactformprocess3.php" method="post" class="myForm" >
<input name="name" type="text" id="f1" class="name-textinput"/><br />
<script type="text/javascript">
var f1 = new LiveValidation('f1');
f1.add( Validate.Presence );
</script>
<input name="email" type="text" id="f21" class="email-textinput"/><br />
<script type="text/javascript">
var f21 = new LiveValidation('f21');
f21.add( Validate.Presence );
f21.add( Validate.Email );
f21.add( Validate.Length, { minimum: 10, maximum: 35 } );
</script>
<textarea name="message" rows="9" cols="34" id="field3" class="message-textinput"></textarea>
<span class="underform">
<input type="reset" class="reset" />
<input type="submit" value="submit" class="submit" />
</span>
</form>
AJAX
$(function() {
$('.myForm').submit(function(e) {
e.preventDefault();
var form = $(this);
var post_url = form.attr('action');
var post_data = form.serialize();
$.ajax({
type: 'POST',
url: post_url,
data: post_data,
success: function(msg) {
$('.success-box').fadeIn("slow").delay(3000).fadeOut("slow");
},
error: function(xhr, status, error, req) {
$('.failure-box').fadein("slow").delay(3000).fadeOut("slow");
}
});
});
});
PHP 电子邮件
<?php
$to = 'wj@pieceofmedesigns.com';
$subject = 'Piece of Me Designs';
$from = $_POST['email'];
$name = $_POST['name'];
$message = $_POST['message'];
if (mail($to, $subject, $message, 'From: '.$name.' <'.$from.'>')) {
echo 'Message was sent successfully.';
} else {
echo 'There was a problem sending your message.';
}
?>