2010-07-05 82 views
0

我提交電子郵件提交按鈕後,我正在使用jQuery替換圖像here。這部分工作得很好,但是,我也需要從表單內容中生成電子郵件。jQuery看起來不錯。現在我如何獲得PHP電子郵件發送?

下面的形式:

  <form action="estimate.php" action="post"> 
       <fieldset> 
       <input type="text" name="name" value="FULL NAME" onfocus="if (this.value=='FULL NAME') this.value='';"/> 
       <input type="text" name="phone" value="PHONE NUMBER" onfocus="if (this.value=='PHONE NUMBER') this.value='';"/> 
       <input type="text" name="email" value="EMAIL" onfocus="if (this.value=='EMAIL') this.value='';"/> 
       <input type="text" name="date" value="MOVE DATE" onfocus="if (this.value=='MOVE DATE') this.value='';"/> 
       <input type="text" name="origin" value="ORIGINATING ADDRESS" onfocus="if (this.value=='ORIGINATING ADDRESS') this.value='';"/> 
       <input type="text" name="destination" value="DESTINATION ADDRESS" onfocus="if (this.value=='DESTINATION ADDRESS') this.value='';"/> 
       <select name="move-type"> 
        <option value="" selected="selected">TYPE OF MOVE</option> 
        <option value="Private">Private</option> 
        <option value="Commercial">Commercial</option> 
       </select> 
       <input id="quoteSubmit" 
        type="image" src="_images/btn_submit.png" alt="" 
        onmouseover="javascript:this.src='_images/btn_submit-over.png'" 
        onmouseout="javascript:this.src='_images/btn_submit.png'"/> 
       </fieldset> 
      </form> 

這裏是jQuery的:

 // free quote form 
    $('#freeQuote form').submit(function(e){ 

    //Set the data ready for ajax post 
    var formdata = $(this).serialize(); 

    $.post("estimate.php",formdata,function(data){ 
     if(data.error) 
     { 
      alert('Error: ' + data.error); 
      return; 
     } 
    }); 

    //The Image 
    var Image = $('<img />').attr({src:'_images/free-quote-confirm.png', width:231, height:267, alt:"Success"}); 

    //Remove the form 
    $('#freeQuote form').remove() 

    //Add the image inside the div 
    $('#freeQuote').append(Image); 

    //Return false so the form does not send as normal. you can also use e.PreventDefault(): 
    return false; 
}); 

這裏是PHP:

<?php # sends contents of Free Estimate form 

if (isset($_POST['submitted'])) { 
$errors = array(); 

// Check for empty fields 

if (empty($_POST['name'])) { 
    $errors[] = 'Please enter your name.'; 
} 

if (empty($_POST['email'])) { 
    $errors[] = 'Please enter your email address.'; 
} 

if (empty($_POST['date'])) { 
    $errors[] = 'Please enter the date of your move.'; 
} 

if (empty($_POST['origin'])) { 
    $errors[] = 'Please enter the origin of your move.'; 
} 

if (empty($_POST['destination'])) { 
    $errors[] = 'Please enter the destination.'; 
} 

if (empty($errors)) { // everything is okay 

$body = "The following Free Estimate Request has been submitted:\n 

    Submitted by: {$_POST['name']}\r 
    E-mail: {$_POST['email']}\r 
    Phone: {$_POST['phone']}\r 
    Move date {$_POST['date']}\r 
    Moving from: {$_POST['origin']}\r 
    Moving to: {$_POST['destination']}\r 
    Move type: {$_POST['move-type']}\r; 

    mail ('[email protected]', 'Free Estimate Request', $body, 'From: [email protected]');  

    // end email 

} else {  
    echo '<h2>Error!</h2> 
    <p class="errors">The following error(s) occurred:<br />'; 
    foreach ($errors as $msg) { 
     echo " - $msg<br />\n"; 
    } 
    echo '</p><p>Please go back and try again.</p><p><br /></p>'; 
} 
}; 

?> 

的基本問題是,我可以點擊提交按鈕沒有任何輸入的字段,我沒有收到電子郵件。

任何幫助,將不勝感激。

謝謝。

+0

正確配置您的郵件守護進程?嘗試一個精簡的郵件程序腳本。 – erenon 2010-07-05 17:36:35

+0

有點偏題,至少對於這個問題,重構那個錯誤代碼!!! – 2010-07-05 17:40:25

+0

然而,如果我知道答案,我不會問這些問題,但是你的意見是值得讚賞的。我在這裏學習! – fmz 2010-07-05 17:59:40

回答

2

除非你的代碼剪斷是正確的,你有以下的問題..

$body = " // there is no closing " 

也說明你的問題:

的基本問題是,我可以點擊提交按鈕而不任何在字段中輸入的東西,我沒有收到電子郵件。

你的例子會建議(如果沒有語法錯誤),你不想發送電子郵件,如果有錯誤。

新修正request.php

<?php # sends contents of Free Estimate form 

if (isset($_POST['submitted'])) { 
    $errors = array(); 

    // Check for empty fields 

    $checkArray = array('name', 'email', 'date', 'origin', 'destination'); 
    foreach($checkArray as $check) { 
     if (empty($_POST[$check])) { 
      $errors[] = 'Please enter your '.$check; 
     } 
    } 

    if (empty($errors)) { // everything is okay 

     $body = "The following Free Estimate Request has been submitted:\n 

      Submitted by: {$_POST['name']}\r 
      E-mail: {$_POST['email']}\r 
      Phone: {$_POST['phone']}\r 
      Move date {$_POST['date']}\r 
      Moving from: {$_POST['origin']}\r 
      Moving to: {$_POST['destination']}\r 
      Move type: {$_POST['move-type']}\r; 

     "; 

     mail ($to, 'Free Estimate Request', $body, 'From: '.$from);  

     // end email 

    } else {  
     // If error then NO Email sent 
     echo '<h2>Error!</h2> 
     <p class="errors">The following error(s) occurred:<br />'; 
     foreach ($errors as $msg) { 
      echo " - $msg<br />\n"; 
     } 
     echo '</p><p>Please go back and try again.</p><p><br /></p>'; 
    } 
}; 

?> 
+0

蜥蜴,感謝您對php代碼的幫助。我把它放在原處並添加了一個$變量,但是,我仍然沒有收到一封電子郵件。沒有錯誤,但沒有電子郵件。 – fmz 2010-07-05 17:58:19

+0

你接受了,所以你理清了嗎? – Lizard 2010-07-05 18:09:26