2014-10-17 21 views
0

我想將選定的對象發送到一個電子郵件地址。PHP通過電子郵件向用戶發送選定的變量

這裏是一個演示: http://jsfiddle.net/62g3s8sz/

我知道電子郵件可以用PHP來發送和我試過很多方法,但都沒有成功。

電子郵件需要與所選項目對應。

所以,如果酷冷至尊K-380採用AMD FM2 A6-6400K雙核3,9GHz選擇我需要它列出這些項目

案例:酷冷至尊K-380:價格 處理器:AMD FM2 A6 -6400K雙核3,9GHz:價格

然後將其發送到該人登錄的電子郵件地址。 我知道$ loggedInUser->電子郵件會顯示電子郵件地址。

如果有人知道如何在電子郵件中發送所有這些信息到登錄人員那將是非常棒的。

HTML:

<script type="text/javascript" src="js/formcalc.js"></script> 

<form method="POST" name="contactform" action="contact-form.php" id="computerform"> 
    <div> 
     <div class="cont_order"> 
      <fieldset> 
       <legend></legend> 
       <label>Case</label> 
       <br> 
       <label class='radiolabel'> 
        <input checked="checked" type="radio" name="selectedcase" value="2001" onclick="calculateTotal()" />Cooler Master K-350 - ($10)</label> 
       <br/> 
       <label class='radiolabel'> 
        <input type="radio" name="selectedcase" value="2002" onclick="calculateTotal()" />Cooler Master K-380 - ($20)</label> 
       <br/> 
       <br/> 
       <label>Processor</label> 
       <br> 
       <label class='radiolabel'> 
        <input checked="checked" type="radio" name="selectedprocessor" value="3001" onclick="calculateTotal()" />AMD FM2 A4-5300 Dual Core 3,4GHz - ($10)</label> 
       <br/> 
       <label class='radiolabel'> 
        <input type="radio" name="selectedprocessor" value="3002" onclick="calculateTotal()" />AMD FM2 A6-6400K Dual Core 3,9GHz - ($20)</label> 
       <br/> 
       <br/> 
       <label>Totale price</label> 
       <div class="total"> 
        <div id="case"></div> 
        <div id="totalPrice"></div> 
       </div> 
       <br> 
      </fieldset> 
     </div> 
     <input type='submit' id='submit' value='Bestel' onclick="calculateTotal()" /> 
    </div> 
</form> 

JavaScript代碼

var case_prices = new Array(); 
case_prices["2001"]=10; 
case_prices["2002"]=20; 

var processor_prices = new Array(); 
processor_prices["3001"]=10; 
processor_prices["3002"]=20; 




window.onload = function() 
{ 
    calculateTotal(); 
} 

// getCasePrice() finds the price based on the selected case 
// Here, we need to take user's the selection from radio button selection 
function getCasePrice() 
{ 
    var casePrice=0; 
    //Get a reference to the form id="computerform" 
    var theForm = document.forms["computerform"]; 
    //Get a reference to the case the user Chooses name=selectedcase": 
    var selectedCase = theForm.elements["selectedcase"]; 
    //We loop through each radio buttons 
    for(var i = 0; i < selectedCase.length; i++) 
    { 
     //if the radio button is checked 
     if(selectedCase[i].checked) 
     { 
      //we set cakeSizePrice to the value of the selected radio button 
      //i.e. if the user choose the 8" cake we set it to 25 
      //by using the cake_prices array 
      //We get the selected Items value 
      //For example cake_prices["Round8".value]" 
      casePrice = case_prices[selectedCase[i].value]; 
      //If we get a match then we break out of this loop 
      //No reason to continue if we get a match 
      break; 
     } 
    } 
    //We return the cakeSizePrice 
    return casePrice; 
} 


// getCasePrice() finds the price based on the selected case 
// Here, we need to take user's the selection from radio button selection 
function getProcessorPrice() 
{ 
    var processorPrice=0; 
    //Get a reference to the form id="computerform" 
    var theForm = document.forms["computerform"]; 
    //Get a reference to the cake the user Chooses name=selectedprocessor": 
    var selectedProcessor = theForm.elements["selectedprocessor"]; 
    //We loop through each radio buttons 
    for(var i = 0; i < selectedProcessor.length; i++) 
    { 
     //if the radio button is checked 
     if(selectedProcessor[i].checked) 
     { 
      //we set cakeSizePrice to the value of the selected radio button 
      //i.e. if the user choose the 8" cake we set it to 25 
      //by using the cake_prices array 
      //We get the selected Items value 
      //For example cake_prices["Round8".value]" 
      processorPrice = processor_prices[selectedProcessor[i].value]; 
      //If we get a match then we break out of this loop 
      //No reason to continue if we get a match 
      break; 
     } 
    } 
    //We return the cakeSizePrice 
    return processorPrice; 
} 


function calculateTotal() 
{ 
    //Here we get the total price by calling our function 
    //Each function returns a number so by calling them we add the values they return together 
    var computerPrice = getCasePrice() + getProcessorPrice() + getCoolerPrice() + getMotherboardPrice(); 

    //display the result 
    var divobj = document.getElementById('totalPrice'); 
    divobj.style.display='block'; 
    divobj.innerHTML = "Totale prijs: &euro;"+computerPrice ; 
} 

謝謝:)

+0

請包括'接觸form.php'的相關內容,讓我們知道具體是什麼出了問題。 – showdev 2014-10-17 21:01:46

+0

我不能添加這個,因爲沒有。但這不是我需要知道如何使這些變量在contact-form.php中工作的問題 – JeEmGu 2014-10-18 20:48:57

回答

0

我不喜歡只粘貼鏈接到答案,但...

http://php.net/manual/en/function.mail.php

因此,所有你需要的是這樣的事情

$content = "..write it here yadda yadda whatever is your mail content .."; 
$ok = mail($mailTo,"Hello, this is the mail subject",$content); 

注意這將發送純郵件,有幾個額外的步驟,以使HTML郵件,全部覆蓋上面的鏈接。祝你好運

很少有提醒:它可能不會在你的本地計算機上工作,除非你有一個安裝了PHP的郵件守護進程並且你可能需要調整該郵件()函數的最後一個參數,這取決於PHP已安裝。

0

PHP代碼必須在contact-form.php,或者您必須在HTML中更改該名稱。 使用PHP的mail()函數。

在PHP中,表格數據可用於超全局變量$_REQUEST。 試試print_r($_REQUEST);看看裏面有什麼。

0

在php中,您通過mail()函數發送郵件(或者您可以使用外部郵件程序插件),這需要一些基本概念。示例:

$to = "[email protected]"; 
    $subject = "You just made an order"; 
    $body = $message; 
    $headers = "MIME-Version: 1.0" . "\r\n"; 
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; 
    $headers .= "From: [email protected]"; 

    if (mail ($to, $subject, $body, $headers)) { 
     echo "1"; 
    } 

快速解釋,您需要幾個參數才能使函數正常工作。我只是從我自己的郵件功能中移除了一些項目,但我會解釋。你需要一個目標($to變量)一個主題($subject變量和內容,我在這裏定義爲$body ...現在我也添加了標題,這是額外的,但對於你來說,這可能很重要。行允許你在電子郵件中使用HTML,你可以用它來標記你的郵件,比如使用表格,你也可以指定電子郵件的來源地址

我使用echo來檢查它是否通過mail功能,如果是這樣,我使用該參數在ajax調用返回來放置一個附加的div。

請記住,調用一個外部樣式表是不是在這個工作,所以,你必須做你的元素的內聯樣式

編輯

我看到你正在試圖從你的字段集了在電子郵件中得到總計。由於您使用div來顯示最終內容,因此發送表單不會讓您從總量中傳輸變量。 (順便說一句,如果你提交這個表單,你實際上以3002和2001作爲變量,而不是電子郵件中的名字,因此你需要做一些查詢工作來推出正確的名稱)因爲我認爲這些價格是同樣在數據庫中,您可以從那裏計算固定的價格,並在查詢中將其推出,然後您必須在$body變量中使用該值。

嘗試你指出正確的方向

相關問題