我有一个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
函数相关联,然后从那里发送电子邮件。无论如何,我怎么能这样做?
-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 :(得分:0)
编写一个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发送电子邮件。
答案 1 :(得分:0)
我找到了一个很好的解决方案来满足我的需求。我得到了所有的检查&#39;值和我创建一个主题的电子邮件。
所以,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>
所以我有我的所有复选框&#39;有价值和名字。我做了一些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();
});
这就好了。但我收到了该电子邮件中的所有复选框,甚至没有选中复选框。如果我不必打开客户端邮件,只需从我的服务器自动发送邮件,这可能是完美的。
此变体的任何解决方案?