我剛開始學習php的基礎知識,並且想要了解w3schools類的內容。如何使用jQuery驗證在php中發送電子郵件
我的HTML看起來像這樣:
<form role="form" method="POST" id="main-contact-form">
<br style="clear:both">
<h3 style="margin-bottom: 25px; text-align: center;">Contact a Conveyancing Property Lawyer Now</h3>
<div class="form-group">
<input type="text" class="form-control" id="name" name="name" placeholder="Name" required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="email" name="email" placeholder="Email" required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="mobile" name="mobile" placeholder="Mobile Number" required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="subject" name="subject" placeholder="Subject" required>
</div>
<div class="form-group">
<textarea class="form-control" type="textarea" id="message" placeholder="Message" maxlength="140" rows="7" name="message"></textarea>
<span class="help-block"><p id="characterLeft" class="help-block ">You have reached the limit</p></span>
</div>
<button type="submit" id="submit" name="submit" class="btn btn-primary">Submit</button>
</form>
然後我的jQuery的文件看起來像這樣:
$('#main-contact-form').submit(function(event){
event.preventDefault();
$.ajax({
type:'post',
url:'sendememail.php',
data:$(this).serialize(),
success:function(response){
if(response==1)
{
setInterval(function(){$('.review_form').html('<h5><center><div class="alert alert-success">Review Successfully Submitted......</div></center></h5>');},5);
}
else
{
setInterval(function(){$('.review_form').html('<h5><center><div class="alert alert-danger">Sorry Your Review Was Not Submitted......</div></center></h5>');},5);
}
}
});
});
從我能看到,這是多麼或多或少如何郵件被髮送出去。
<?php
$to = "[email protected]";
$subject = "My subject";
$txt = "Hello world!";
$headers = "[email protected]" . "\r\n" .
"CC: [email protected]";
mail($to,$subject,$txt,$headers);
?>
我sendemail.php看起來是這樣的:
<?php
extract($_POST);
$to = $email;
$subject = $subject;
$txt = $message;
$headers = "[email protected]" . "\r\n" .
"CC: [email protected]";
mail($to,$subject,$txt,$headers);
?>
當我點擊提交按鈕,我得到,你可以在圖片中看到的錯誤:
希望能對你有所幫助。
在此先感謝
要將數據從表單發送到php而無需刷新頁面,您需要使用AJAX http://api.jquery.com/jquery.ajax/ –