>我有一个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>
创建邮件.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();
});
这个没关系。但是我在那封电子邮件中收到了所有复选框,甚至没有选中。如果我不必打开客户端邮件,只需自动从我的服务器发送该邮件,这可能是完美的。
这种变化有什么解决方案吗?