I want to pass an array from JS to PHP and then use it there, I gather the values in JS and send them to a fnother file where I try turn it into a $_SESSION variable but when I do a var_dump on this it gives me a string with comma seperated values. Is there a better way of doing this?
My JS:
var value_1 = document.getElementById("value_1").value;
var value_2 = document.getElementById("value_2").value;
var value_3 = document.getElementById("value_3").value;
var value_4 = document.getElementById("value_4").value;
var value_5 = document.getElementById("value_5").value;
var values = [];
values.push(value_1);
values.push(value_2);
values.push(value_3);
values.push(value_4);
values.push(value_5);
var formData = new FormData();
formData.append("values", values);
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
if(this.readyState == 4 && this.status == 200){
// success
}
};
xmlhttp.open("POST", "myfile.php", true);
xmlhttp.send(formData);
PHP:
if(isset($_POST['values'])){
$_SESSION['values'] = $_POST['values'];
}