简体   繁体   English

如何从JavaScript数组访问元素以获取使用PHP / HTML发送的电子邮件

[英]How to access elements from a javascript array for an email sent with PHP/HTML

So I am working on a webpage that generates a javascript array from the date element in html. 所以我正在一个网页上,该网页从html中的date元素生成一个javascript数组。

That array itself works fine, and I am able to print it to the webpage by using a command like this: 该数组本身可以正常工作,我可以使用以下命令将其打印到网页上:

document.getElementById("demo").innerHTML = week[0];

to access the "week" array generated within javascript and to paste it to an HTML span element on the webpage. 访问javascript中生成的“周”数组并将其粘贴到网页上的HTML span元素。

However, this is a webpage that sends emails using the data that the user inputs to the page, and while I am able to get all of the rest of the data from the user, I am unsure as to how to grab the data from this javascript array for the email. 但是,这是一个使用用户输入到页面的数据发送电子邮件的网页,尽管我能够从用户那里获取所有其余数据,但是我不确定如何从该页面获取数据电子邮件的javascript数组。

The email is currently formatted using HTML with php variables. 该电子邮件当前使用带有php变量的HTML格式化。 I thought about using a php array, but every solution I look up for that seems either to be for a different case or too complicated for me to dissect and repurpose. 我考虑过使用php数组,但是我所寻找的每个解决方案似乎都是针对不同的情况,或者太复杂了,以至于我无法剖析和调整用途。

So what I need is a solution like one of the following: 因此,我需要的是一种类似于以下解决方案的解决方案:

-The ability to copy this javascript array into a php array that I can then access for the email. -可以将此javascript数组复制到php数组中,然后可以访问该电子邮件数组。

-The ability to access this javascript array within the HTML for the email -能够在电子邮件的HTML中访问此javascript数组

This is all contained within a php file on server (the HTML markup, the javascript functions and the php). 这些全部包含在服务器上的php文件中(HTML标记,javascript函数和php)。

Thank you, 谢谢,

Michael 迈克尔

EDIT: I found this thread: Pass Javascript Array -> PHP and tried their solution with this code: 编辑:我发现此线程: 通过Javascript数组-> PHP,并尝试使用此代码的解决方案:

Javascript: 使用Javascript:

JSON.stringify(week);

PHP: PHP:

$datesOfWeek = json_decode($_POST['week']);

But whenever I try to access the elements of the array and print them out in the email, nothing prints out for these elements. 但是,每当我尝试访问数组的元素并在电子邮件中将其打印出来时,这些元素都不会打印出来。

I'm not sure what you exactly want to achieve but if you want to send a data from js (on client) along with form data to php script (on server) you should simply serialize your array and put as value of additional (hidden) form field. 我不确定您到底想实现什么,但是如果您想从js(在客户端)上将数据与表单数据一起发送到php脚本(在服务器上),则应该简单地序列化数组并将其作为附加值(隐藏) )表单字段。 In php script you can somehow deserialize a value of that field (it depends on the format of you choice) 在php脚本中,您可以以某种方式反序列化该字段的值(取决于您选择的格式)

//EDIT: Here is js script example that can you use http://jsfiddle.net/zyt2kcmv/ //编辑:这是js脚本示例,您可以使用http://jsfiddle.net/zyt2kcmv/

var myArrayToSend = ["val1", "val2"]
var arrayField = document.querySelector("[name=jsArray]");
arrayField.value = JSON.stringify(myArrayToSend)

On the server side you just need to deserialize value of $_POST["jsArray"] (use library for json or simple regex). 在服务器端,您只需要反序列化$ _POST [“ jsArray”]的值(对json或简单正则表达式使用库)。 Instead of JSON.stringify you can use myArrayToSend.join(";") and then use split method in pho to "deserialize" that string. 可以使用myArrayToSend.join(“;”)代替JSON.stringify,然后在pho中使用split方法对字符串进行“反序列化”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM