Php 将非表单javascript数组数据发送到电子邮件正文
我有以下功能,这是更大的单页应用程序的一部分Php 将非表单javascript数组数据发送到电子邮件正文,php,javascript,jquery,ajax,json,Php,Javascript,Jquery,Ajax,Json,我有以下功能,这是更大的单页应用程序的一部分 gatherData function(){ var superarray = [] var row = 0 $('ul li').each(function(){ var task = $(this).find('.edited').html(); var time = $(this).find('.time').html(); var array = []
gatherData function(){
var superarray = []
var row = 0
$('ul li').each(function(){
var task = $(this).find('.edited').html();
var time = $(this).find('.time').html();
var array = []
var count = 0
array[count] = task;
count = count + 1
array[count] = time;
superarray[row] = array
row = row + 1
}); // end each
console.log(superarray); // this is the data i want tabulated in email
});
我希望使用
<form><email><submit class="send"></form>
$('.send').click(function(){
gatherData();
$.post("processform.php", // now I'm stuck
$('.send')。单击(函数(){
收集数据();
$.post(“processform.php)”,//现在我被卡住了
通过php/ajax/json/的最合适组合发送电子邮件,该组合将生成以下格式的消息
“您收集数据的结果如下:
任务1时间1
任务2时间2
任务
角色学分等”
在我无知的情况下,我曾想过创建一个隐藏的消息字段,并在发布之前通过DOM将数据添加到该字段中。然而,我认为必须有一种更优雅的方法
有人能为我推荐这方面的最佳实践(或工作代码),并为我指出哪种技术/技术最适合这种情况。谢谢
你会这样说:
$('.send').click(function(){
var data = gatherData();
$.post("processform.php?data="+ data) .....
在php脚本中,您只需使用
echo$\u POST['data']
@happilyunstack,更好的方法可能是执行以下操作:
在单击事件中,提示用户确认他们已准备好提交表单。根据返回值,
继续你的工作
在您的帖子中:
a、 )将原来的$post放回原样
b、 )将表单中的数据序列化为json…将.serialize()替换为.serializeArray()
c、 )在对$.post的调用中丢失该函数…除非要报告成功或失败,否则不需要它
在服务器端,使用json库对post数据进行反序列化,并根据需要使用。这种类型的应用程序/用例正是json的用意…看起来很有用,尽管我在理解上仍有困难。声明变量数据也会运行该函数吗?(功能取决于只有当用户对页面满意时才会发生这种情况)您也能解释一下$.post方法的工作原理吗?我想我了解一些如何将数据添加到post中,尽管我最初的ajax是
$.post(“processform.php”,$(“#mycontactform”).serialize(),function(data){})
我不明白您的声明如何与此结合,以发送输入的电子邮件和数据。很多感谢您,相信我,我已经使用过数百万次类似的帖子。您只需在php文件中获得分隔字符串(&S)。gatherData应返回您的数组。