I am building an array in javascript, using JSON.stringify() on it, and then setting a hidden input field's value attribute to the resulting string.
function setSelectedArray(){
var ticketInfo = new Array(); //creates new array for later
var num=0; //num accumulates the number of the next row
for(var i=0;i<hlRows.length;i++){
if(hlRows[i]){ //hlRows is an array with boolean values telling me
//which dataArray values to set ticketInfo to
ticketInfo[num] = dataArray[i];
//dataArray is a pre-defined array and contains a large amount of data
//ticketInfo is a smaller array that is gathering all data specified from hlRows
num++;
}
}
//line below sets 'tix'(hidden input field).value to the JSON version of ticketInfo
document.getElementsByName('tix').value = (JSON.stringify(ticketInfo));
//At this point, my alert below displays everything I want to see.
//It looks like correct JSON to me
alert("The VALUE of 'tix' is: "+document.getElementsByName('tix').value);
//this submits the form that the hidden field is inside of.
//it wouldn't go to the next page(which it does) without
//this calling the form's action attribute correctly (i think..)
document.forms["hiddenForm"].submit();
}
You can read my comments if you need more description. Here is the HTML part of the process:
<!-- This button, when clicked, calls the setSelectedArray() function -->
<input class="button" type="button" id="btnContact" value="Select Tickets" onclick="setSelectedArray()" style="width:20em;float:right;"/>
<form id="hiddenForm" action="email.php" method="post">
<input type="hidden" name="tix" value=""/> <!-- I tried this with no value specified too -->
</form>
I don't know how many different things I tried in email.php to just get any sort of output. I tried echos and print and print_r and print from loops and printing values inside the "array", but nothing seemed to work. Here is where email.php is at right now:
<?php
$jsArray = json_decode($_POST['tix']);
var_dump($jsArray);
?>
The only output I get from email.php is "NULL"(which it wasn't 2 seconds earlier in the alert) or "Array". Otherwise, it says nothing and I just get an empty screen. What am I doing wrong here? Do I need to use AJAX to post the data? I feel like that isn't right.
<input type="hidden" name="tix" id="tix" value=""/>