2016-01-21 39 views
0

我有一个HTML表单,从复选框检索某些选项:发送Javascript数组在邮件

 <form action="" method="post" id="menuform" name="menuform"> 
     <fieldset> 
      <legend>Select a Menu</legend> 
      <label> 
       <input type="radio" name="selectedmenu" checked value="menu01" ape-qty='8' ent-qty='1' pes-qty='1' sor-qty='1' car-qty='1' pos-qty='1' data-price='95' /> 
       <span>First Item</span> 
      </label> 
      ...(more)... 
     </fieldset> 
     <fieldset> 
      <legend id='aperitivosPrompt'>Elegir Aperitivos</legend> 
      <label> 
       <input type='checkbox' name='aperitivos' value="1" /> 
       <span>Item 1</span> 
      </label> 
      <label> 
       <input type='checkbox' name='aperitivos' value="2" /> 
       <span>Item 2</span> 
      </label> 
      <label> 
       <input type='checkbox' name='aperitivos' value="3" /> 
       <span>Item 3</span> 
      </label> 
      ...(more)... 
     </fieldset>    
     <fieldset> 
      <input type="submit" value="Enviar" /> 
     </fieldset> 
    </form> 

然后我送这个变量的JavaScript文件,我保存所有复选框选项在多个阵列

var menuform = document.getElementById('menuform'), 
    radios = document.getElementsByName('selectedmenu'), 
    aperitivos = document.getElementsByName('aperitivos'), 
    aperitivosPrompt = document.getElementById('aperitivosPrompt'), 
    totalPrice = document.getElementById('totalPrice'), 
    result = document.getElementById('result'), 
    aperitivosAllowed = 0, 
    currentSelectionAperitivos = [], 
    currency = '€'; 

function handleAperitivos(e) { 
    var count = getSelectedCountApe(); 
    if (count > aperitivosAllowed) { 
     resetSelectApe(); 
    } else { 
     currentSelectionAperitivos = getSelectedValuesApe(); 
    } 
} 
...(more)... 
function getSelectedCountApe() { 
    var count = 0; 
    for (var i = 0; i < aperitivos.length; i++) { 
     if (aperitivos[i].checked) count++; 
    } 
    return count; 
} 
...(more)... 

那么,如何在电子邮件中发送这些名为currentSelectionAperitivos = []的阵列呢?我希望在用户选择所有选项后,我会在收件箱中收到一封电子邮件,其中包含选定的选项。

我认为我必须连接此表格和JS文件与PHP函数,并从那里发送电子邮件。无论如何,我该如何做到这一点?

-EDITED-

我试图用JSON的解决方案:

for (var i = 0; i < aperitivos.length; i++) { 
    var item = { 
     "valor": i, 
     "etiqueta": aperitivos[i].value 
    }; 
    currentSelectionAperitivos.push(item); 
} 
currentJSON = JSON.stringify({currentSelectionAperitivos:currentSelectionAperitivos}); 
console.log(currentJSON); 

所以现在我在浏览器控制台中看到所有的 “值” 从田地HTML表单的<input>。但不仅CHECKED值,而且我现在需要的是获取此JSON.stringify并通过邮件使用PHP发送它。

+0

正在处理表单的php在哪里? – jeroen

+0

研究如何通过电子邮件发送表格数据与PHP –

+0

签出phpmailer。其中一个最简单的PHP电子邮件插件那里https://github.com/PHPMailer/PHPMailer –

回答

0

编写一个PHP脚本来发送电子邮件,更改表单动作以在此脚本上发布。

注意:这是未经测试的。

您的形式更改为:

<form action="mail.php"...> 
</form> 

<?php 
    ob_start(); 
    var_dump($_POST); 
    $result = ob_get_clean(); 
    mail ('[email protected]' , 'subject' , $result); 
?> 

创建mail.php否则,你可以使用JSON.stringify和API与JS发送电子邮件。

+0

不适合我那简单的解决方案。点击发送按钮时没有任何反应。 –

+0

似乎var_export没有返回一个字符串,因为我会这样做。上面编辑的例子确实有效。如果你想获得多个复选框的状态,你应该使用'name ='aperitivos []''代替。 – christian

0

我找到了一个很好的解决方案来满足我的需求。我得到所有“检查”的值,并创建一个包含主题的电子邮件。

所以,HTML表单:

<html> 
<head> 
    <script type='text/javascript' src='http://code.jquery.com/jquery-1.9.1.js'></script> 
    <script type='text/javascript' src='http://sisyphus-js.herokuapp.com/assets/application-09059f9f54cc3c3bff98487cf866644b.js'></script> 
</head> 

<body> 
    <form action="configurador.php" method="post" id="menuform" name="menuform"> 
     <fieldset> 
      <legend id='itemPrompt'>Choose Item</legend> 
      <label> 
       <input type='checkbox' name='item' value="Value#01" /> 
       <span>Value#01</span> 
      </label> 
      (...more...) 
      <input type="submit" name="submit" value="Send" /> 
     </fieldset> 
    </form> 
</body> 

所以有我有我所有的 '复选框' 用价值观和名称。我做了一些JavaScript功能,但最重要的是这一次,我检索所有值和发送主题的电子邮件:

function sendMail() { 
    var mailbody = "My Configurator: \n\n" 
        + $('input[name="item"]') 
        .map(function(id, box){ return (box.checked ? "[x]" : "[_]") + " " + box.value;}) 
        .get() 
        .join("\n"); 
    var link = "mailto:[email protected]" 
       + "?cc=" 
       + "&subject=" + escape("Configurator - Subject") 
       + "&body=" + escape(mailbody); 
    window.location.href = link; 
} 

$(function(){ 
    $("#menuform").sisyphus(); 
}); 

而且它的确定只此。但是,我收到了该电子邮件中的所有复选框,即使没有选中。如果我不必打开客户邮件,只需从我的服务器自动发送邮件即可。

这种变化的任何解决方案?