0

So I am working on a webpage that generates a javascript array from the date element in html.

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.

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.

The email is currently formatted using HTML with php variables. 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.

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.

-The ability to access this javascript array within the HTML for the email

This is all contained within a php file on server (the HTML markup, the javascript functions and the php).

Thank you,

Michael

EDIT: I found this thread: Pass Javascript Array -> PHP and tried their solution with this code:

Javascript:

JSON.stringify(week);

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.

Community
  • 1
  • 1
Michael Anthony Leber
  • 365
  • 2
  • 10
  • 24
  • Is [this](http://stackoverflow.com/questions/5035547/pass-javascript-array-php) wha you are looking for ? – Rohil_PHPBeginner Oct 10 '14 at 18:05
  • Well, I'm not sure. I went into that and used: "JSON.stringify(week);" in the javascript and: "$datesOfWeek = json_decode($_POST['jsondata']);" later on in the php, but whenever I try to print any of the array out, there's nothing. – Michael Anthony Leber Oct 10 '14 at 18:21
  • I think you have made mistake with POST.Try to put your array in place of jsondata. – Rohil_PHPBeginner Oct 10 '14 at 18:25
  • Alright, I tried that, but it still doesn't work. It sends the email and everything, but where that element should print there is nothing. – Michael Anthony Leber Oct 10 '14 at 18:35
  • How did you use that to print ? – Rohil_PHPBeginner Oct 10 '14 at 18:38
  • Well I took the $datesOfWeek array and saved the first value into a php variable like this: "$dateMonday = $datesOfWeek[0];" Then in the html email I wrote "$dateMonday" into the message (this is all using PHP's mail function, so this particular portion was within a PHP variable called "$email_body"). – Michael Anthony Leber Oct 10 '14 at 18:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/62844/discussion-between-michael-anthony-leber-and-rohil-phpbeginner). – Michael Anthony Leber Oct 10 '14 at 18:44

1 Answers1

1

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. In php script you can somehow deserialize a value of that field (it depends on the format of you choice)

//EDIT: Here is js script example that can you use 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). Instead of JSON.stringify you can use myArrayToSend.join(";") and then use split method in pho to "deserialize" that string.

cezarypiatek
  • 1,078
  • 11
  • 21
  • What I want is to print the values at the individual indexes of the javascript array into the email at specified points. I've heard solutions like yours above before, but again I would have no idea how to implement that. – Michael Anthony Leber Oct 10 '14 at 18:02
  • Ok I tried this: "var arrayField = document.querySelector("[name=jsArray]");" and this: "arrayField.value = JSON.stringify(week);" in the javascript, and I get this error in my chrome console: "Uncaught TypeError: Cannot set property 'value' of null" for the "arrayField.value" line. – Michael Anthony Leber Oct 10 '14 at 18:39
  • Look at the jsFiddle sample. jsArray is the name of form field (form that is posted to PHP script). The reason why you're getting this error is that you simply don't have that field in your form. Adding field with that name should fix that problem. – cezarypiatek Oct 10 '14 at 18:46