[英]Send a Javascript array in a mail
我有一个HTML表单,可以从复选框中检索一些选项:
<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
函数连接,并从那里发送电子邮件。 无论如何,我该怎么做?
-编辑-
我尝试使用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用邮件发送。
编写一个PHP脚本来发送电子邮件,更改表单操作以在此脚本上发布。
注意:这未经测试。
将表格更改为:
<form action="mail.php"...>
</form>
创建mail.php像
<?php
ob_start();
var_dump($_POST);
$result = ob_get_clean();
mail ( 'your@email.com' , 'subject' , $result);
?>
否则,您可以使用JSON.stringify和API来发送带有JS的电子邮件。
我找到了满足自己需求的好解决方案。 我得到所有“已检查”值,并创建带有主题的电子邮件。
因此,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:mail@mail.com"
+ "?cc="
+ "&subject=" + escape("Configurator - Subject")
+ "&body=" + escape(mailbody);
window.location.href = link;
}
$(function(){
$( "#menuform" ).sisyphus();
});
这样做就可以了。 但是我在该电子邮件中得到了所有复选框,甚至没有选中。 如果我不必打开客户邮件,而只是从我的服务器自动发送该邮件,那可能是完美的。
这个变化有什么解决办法吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.