2017-06-21 37 views
0

我剛開始學習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); 
?> 

當我點擊提交按鈕,我得到,你可以在圖片中看到的錯誤:

enter image description here

希望能對你有所幫助。

在此先感謝

+0

要將數據從表單發送到php而無需刷新頁面,您需要使用AJAX http://api.jquery.com/jquery.ajax/ –

回答

0

試試這個代碼:

創建像sendemail.php並輸入你的電子郵件的代碼發送電子郵件新的一頁。

從窗體中移除操作並在窗體標籤中添加屬性標識。

<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的改變:

<script> 
$('#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); 

     } 
    } 

    }); 
    }); 
</script> 

這裏你sendemail.php:

<?php 
extract($_POST); 
$to = $email; 
$subject = $subject; 
$txt = $message; 
$headers = "[email protected]" . "\r\n" . 
"CC: [email protected]"; 

mail($to,$subject,$txt,$headers); 
?> 

電子郵件是發送給您的電子郵件文本框中輸入。

+0

但是如果我希望將電子郵件發送給我,我該如何處理。說我的電子郵件是[email protected]。我的PHP怎麼樣? – Nadj

+0

請參閱我的編輯代碼。 –