Pass Array from JS to PHP and create SESSION variable
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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'];
}
javascript php arrays session
|
show 2 more comments
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'];
}
javascript php arrays session
Are you sure it gives you a string with comma separated values, or is it more like["value_1", "value_2", "value_3"]
?
– GrumpyCrouton
Jan 4 at 14:39
yeah I dovar_dump($_SESSION['values']);
and it returnsC:wamp64www<directory><file>:<line>:string 'value1,value2,value3,value4,value5
– Paddy Hallihan
Jan 4 at 14:43
You could stringify the array to a json string that you post to PHP, which you then decode in PHP.
– Magnus Eriksson
Jan 4 at 14:43
1
Possible duplicate of Sending Arrays via Ajax/JSON without JQuery
– executable
Jan 4 at 14:47
1
@executable I don't get your flagging as a possible duplicate and an answer. I feel the flag should be removed. That isn't fair for others later who might want to post more answers.
– Funk Forty Niner
Jan 4 at 15:52
|
show 2 more comments
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'];
}
javascript php arrays session
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'];
}
javascript php arrays session
javascript php arrays session
edited Jan 4 at 15:52
Funk Forty Niner
1
1
asked Jan 4 at 14:35
Paddy HallihanPaddy Hallihan
6711625
6711625
Are you sure it gives you a string with comma separated values, or is it more like["value_1", "value_2", "value_3"]
?
– GrumpyCrouton
Jan 4 at 14:39
yeah I dovar_dump($_SESSION['values']);
and it returnsC:wamp64www<directory><file>:<line>:string 'value1,value2,value3,value4,value5
– Paddy Hallihan
Jan 4 at 14:43
You could stringify the array to a json string that you post to PHP, which you then decode in PHP.
– Magnus Eriksson
Jan 4 at 14:43
1
Possible duplicate of Sending Arrays via Ajax/JSON without JQuery
– executable
Jan 4 at 14:47
1
@executable I don't get your flagging as a possible duplicate and an answer. I feel the flag should be removed. That isn't fair for others later who might want to post more answers.
– Funk Forty Niner
Jan 4 at 15:52
|
show 2 more comments
Are you sure it gives you a string with comma separated values, or is it more like["value_1", "value_2", "value_3"]
?
– GrumpyCrouton
Jan 4 at 14:39
yeah I dovar_dump($_SESSION['values']);
and it returnsC:wamp64www<directory><file>:<line>:string 'value1,value2,value3,value4,value5
– Paddy Hallihan
Jan 4 at 14:43
You could stringify the array to a json string that you post to PHP, which you then decode in PHP.
– Magnus Eriksson
Jan 4 at 14:43
1
Possible duplicate of Sending Arrays via Ajax/JSON without JQuery
– executable
Jan 4 at 14:47
1
@executable I don't get your flagging as a possible duplicate and an answer. I feel the flag should be removed. That isn't fair for others later who might want to post more answers.
– Funk Forty Niner
Jan 4 at 15:52
Are you sure it gives you a string with comma separated values, or is it more like
["value_1", "value_2", "value_3"]
?– GrumpyCrouton
Jan 4 at 14:39
Are you sure it gives you a string with comma separated values, or is it more like
["value_1", "value_2", "value_3"]
?– GrumpyCrouton
Jan 4 at 14:39
yeah I do
var_dump($_SESSION['values']);
and it returns C:wamp64www<directory><file>:<line>:string 'value1,value2,value3,value4,value5
– Paddy Hallihan
Jan 4 at 14:43
yeah I do
var_dump($_SESSION['values']);
and it returns C:wamp64www<directory><file>:<line>:string 'value1,value2,value3,value4,value5
– Paddy Hallihan
Jan 4 at 14:43
You could stringify the array to a json string that you post to PHP, which you then decode in PHP.
– Magnus Eriksson
Jan 4 at 14:43
You could stringify the array to a json string that you post to PHP, which you then decode in PHP.
– Magnus Eriksson
Jan 4 at 14:43
1
1
Possible duplicate of Sending Arrays via Ajax/JSON without JQuery
– executable
Jan 4 at 14:47
Possible duplicate of Sending Arrays via Ajax/JSON without JQuery
– executable
Jan 4 at 14:47
1
1
@executable I don't get your flagging as a possible duplicate and an answer. I feel the flag should be removed. That isn't fair for others later who might want to post more answers.
– Funk Forty Niner
Jan 4 at 15:52
@executable I don't get your flagging as a possible duplicate and an answer. I feel the flag should be removed. That isn't fair for others later who might want to post more answers.
– Funk Forty Niner
Jan 4 at 15:52
|
show 2 more comments
3 Answers
3
active
oldest
votes
You should convert your array into JSON, like :
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 json_upload = "values=" + JSON.stringify({values});
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "myfile.php");
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send(json_upload);
PHP
<?php
session_start();
if(isset($_POST['values'])){
$_SESSION['values'] = $_POST['values'];
}
I did this and just changed one line$_SESSION['values'] = json_decode($_POST['values']);
and got the desired result
– Paddy Hallihan
Jan 4 at 15:06
add a comment |
Your problem with your original code was using an array as the value argument for FormData.append() (should be a string, which the JavaScript engine has type-cast your array to in your case).
If you instead append each value with the same key-name suffixed with an empty pair of square brackets (e.g. values
), PHP will automatically form these into a numeric array for you.
JavaScript:
var formData = new FormData();
for(var i = 1; i <= 5; i++) {
formData.append("values", document.getElementById("value_" + i).value);
}
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:
<?php
session_start();
if(isset($_POST['values'])){
$_SESSION['values'] = $_POST['values'];
}
add a comment |
The better way is to use jQuery $.ajax() function:
_this.sendAjaxRequest = function(url, data, callback, errorCallback) {
var _type = $.inArray($.type(data), ['string', 'object']) > -1 ? 'post' : 'get';
var request = $.ajax({
'url' : url,
'type' : _type,
'dataType' : 'json',
'data' : data,
'cache' : false,
'success' : function(response) {
if (typeof callback != 'undefined' && callback) {
callback.call(_this, response);
return;
}
},
'error' : function(response) {
if (typeof errorCallback != 'undefined' && errorCallback) {
errorCallback.call(_this, response);
return;
}
}
});
};
You could use JSON object as data variable.
3
He doesn't use jQuery
– executable
Jan 4 at 14:43
1
"The better way is to use jQuery $.ajax()" - Why would including a 100kb bloated js library be a "better" way?
– Magnus Eriksson
Jan 4 at 14:44
Why is this "better"? Include an external dependency when it's not in use anywhere else?
– GrumpyCrouton
Jan 4 at 14:44
Hi thanks for your answer, I might come back to use jQuery.
– Paddy Hallihan
Jan 4 at 14:45
2
@PaddyHallihan - If you want to learn proper development, I would stick to what you got now and sort the issues out instead. You don't want to be a "library"-developer (a developer that doesn't actually knows the language, but only a few libraries). There's nothing wrong with using tools and libraries, but including a 100kb lib just for this is overkill and bad for the end user (that needs to download all that extra bloat).
– Magnus Eriksson
Jan 4 at 14:48
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54040970%2fpass-array-from-js-to-php-and-create-session-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You should convert your array into JSON, like :
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 json_upload = "values=" + JSON.stringify({values});
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "myfile.php");
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send(json_upload);
PHP
<?php
session_start();
if(isset($_POST['values'])){
$_SESSION['values'] = $_POST['values'];
}
I did this and just changed one line$_SESSION['values'] = json_decode($_POST['values']);
and got the desired result
– Paddy Hallihan
Jan 4 at 15:06
add a comment |
You should convert your array into JSON, like :
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 json_upload = "values=" + JSON.stringify({values});
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "myfile.php");
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send(json_upload);
PHP
<?php
session_start();
if(isset($_POST['values'])){
$_SESSION['values'] = $_POST['values'];
}
I did this and just changed one line$_SESSION['values'] = json_decode($_POST['values']);
and got the desired result
– Paddy Hallihan
Jan 4 at 15:06
add a comment |
You should convert your array into JSON, like :
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 json_upload = "values=" + JSON.stringify({values});
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "myfile.php");
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send(json_upload);
PHP
<?php
session_start();
if(isset($_POST['values'])){
$_SESSION['values'] = $_POST['values'];
}
You should convert your array into JSON, like :
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 json_upload = "values=" + JSON.stringify({values});
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "myfile.php");
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send(json_upload);
PHP
<?php
session_start();
if(isset($_POST['values'])){
$_SESSION['values'] = $_POST['values'];
}
edited Jan 4 at 14:58
answered Jan 4 at 14:51
executableexecutable
1,98321024
1,98321024
I did this and just changed one line$_SESSION['values'] = json_decode($_POST['values']);
and got the desired result
– Paddy Hallihan
Jan 4 at 15:06
add a comment |
I did this and just changed one line$_SESSION['values'] = json_decode($_POST['values']);
and got the desired result
– Paddy Hallihan
Jan 4 at 15:06
I did this and just changed one line
$_SESSION['values'] = json_decode($_POST['values']);
and got the desired result– Paddy Hallihan
Jan 4 at 15:06
I did this and just changed one line
$_SESSION['values'] = json_decode($_POST['values']);
and got the desired result– Paddy Hallihan
Jan 4 at 15:06
add a comment |
Your problem with your original code was using an array as the value argument for FormData.append() (should be a string, which the JavaScript engine has type-cast your array to in your case).
If you instead append each value with the same key-name suffixed with an empty pair of square brackets (e.g. values
), PHP will automatically form these into a numeric array for you.
JavaScript:
var formData = new FormData();
for(var i = 1; i <= 5; i++) {
formData.append("values", document.getElementById("value_" + i).value);
}
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:
<?php
session_start();
if(isset($_POST['values'])){
$_SESSION['values'] = $_POST['values'];
}
add a comment |
Your problem with your original code was using an array as the value argument for FormData.append() (should be a string, which the JavaScript engine has type-cast your array to in your case).
If you instead append each value with the same key-name suffixed with an empty pair of square brackets (e.g. values
), PHP will automatically form these into a numeric array for you.
JavaScript:
var formData = new FormData();
for(var i = 1; i <= 5; i++) {
formData.append("values", document.getElementById("value_" + i).value);
}
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:
<?php
session_start();
if(isset($_POST['values'])){
$_SESSION['values'] = $_POST['values'];
}
add a comment |
Your problem with your original code was using an array as the value argument for FormData.append() (should be a string, which the JavaScript engine has type-cast your array to in your case).
If you instead append each value with the same key-name suffixed with an empty pair of square brackets (e.g. values
), PHP will automatically form these into a numeric array for you.
JavaScript:
var formData = new FormData();
for(var i = 1; i <= 5; i++) {
formData.append("values", document.getElementById("value_" + i).value);
}
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:
<?php
session_start();
if(isset($_POST['values'])){
$_SESSION['values'] = $_POST['values'];
}
Your problem with your original code was using an array as the value argument for FormData.append() (should be a string, which the JavaScript engine has type-cast your array to in your case).
If you instead append each value with the same key-name suffixed with an empty pair of square brackets (e.g. values
), PHP will automatically form these into a numeric array for you.
JavaScript:
var formData = new FormData();
for(var i = 1; i <= 5; i++) {
formData.append("values", document.getElementById("value_" + i).value);
}
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:
<?php
session_start();
if(isset($_POST['values'])){
$_SESSION['values'] = $_POST['values'];
}
answered Jan 4 at 15:51
HeadbankHeadbank
663
663
add a comment |
add a comment |
The better way is to use jQuery $.ajax() function:
_this.sendAjaxRequest = function(url, data, callback, errorCallback) {
var _type = $.inArray($.type(data), ['string', 'object']) > -1 ? 'post' : 'get';
var request = $.ajax({
'url' : url,
'type' : _type,
'dataType' : 'json',
'data' : data,
'cache' : false,
'success' : function(response) {
if (typeof callback != 'undefined' && callback) {
callback.call(_this, response);
return;
}
},
'error' : function(response) {
if (typeof errorCallback != 'undefined' && errorCallback) {
errorCallback.call(_this, response);
return;
}
}
});
};
You could use JSON object as data variable.
3
He doesn't use jQuery
– executable
Jan 4 at 14:43
1
"The better way is to use jQuery $.ajax()" - Why would including a 100kb bloated js library be a "better" way?
– Magnus Eriksson
Jan 4 at 14:44
Why is this "better"? Include an external dependency when it's not in use anywhere else?
– GrumpyCrouton
Jan 4 at 14:44
Hi thanks for your answer, I might come back to use jQuery.
– Paddy Hallihan
Jan 4 at 14:45
2
@PaddyHallihan - If you want to learn proper development, I would stick to what you got now and sort the issues out instead. You don't want to be a "library"-developer (a developer that doesn't actually knows the language, but only a few libraries). There's nothing wrong with using tools and libraries, but including a 100kb lib just for this is overkill and bad for the end user (that needs to download all that extra bloat).
– Magnus Eriksson
Jan 4 at 14:48
|
show 1 more comment
The better way is to use jQuery $.ajax() function:
_this.sendAjaxRequest = function(url, data, callback, errorCallback) {
var _type = $.inArray($.type(data), ['string', 'object']) > -1 ? 'post' : 'get';
var request = $.ajax({
'url' : url,
'type' : _type,
'dataType' : 'json',
'data' : data,
'cache' : false,
'success' : function(response) {
if (typeof callback != 'undefined' && callback) {
callback.call(_this, response);
return;
}
},
'error' : function(response) {
if (typeof errorCallback != 'undefined' && errorCallback) {
errorCallback.call(_this, response);
return;
}
}
});
};
You could use JSON object as data variable.
3
He doesn't use jQuery
– executable
Jan 4 at 14:43
1
"The better way is to use jQuery $.ajax()" - Why would including a 100kb bloated js library be a "better" way?
– Magnus Eriksson
Jan 4 at 14:44
Why is this "better"? Include an external dependency when it's not in use anywhere else?
– GrumpyCrouton
Jan 4 at 14:44
Hi thanks for your answer, I might come back to use jQuery.
– Paddy Hallihan
Jan 4 at 14:45
2
@PaddyHallihan - If you want to learn proper development, I would stick to what you got now and sort the issues out instead. You don't want to be a "library"-developer (a developer that doesn't actually knows the language, but only a few libraries). There's nothing wrong with using tools and libraries, but including a 100kb lib just for this is overkill and bad for the end user (that needs to download all that extra bloat).
– Magnus Eriksson
Jan 4 at 14:48
|
show 1 more comment
The better way is to use jQuery $.ajax() function:
_this.sendAjaxRequest = function(url, data, callback, errorCallback) {
var _type = $.inArray($.type(data), ['string', 'object']) > -1 ? 'post' : 'get';
var request = $.ajax({
'url' : url,
'type' : _type,
'dataType' : 'json',
'data' : data,
'cache' : false,
'success' : function(response) {
if (typeof callback != 'undefined' && callback) {
callback.call(_this, response);
return;
}
},
'error' : function(response) {
if (typeof errorCallback != 'undefined' && errorCallback) {
errorCallback.call(_this, response);
return;
}
}
});
};
You could use JSON object as data variable.
The better way is to use jQuery $.ajax() function:
_this.sendAjaxRequest = function(url, data, callback, errorCallback) {
var _type = $.inArray($.type(data), ['string', 'object']) > -1 ? 'post' : 'get';
var request = $.ajax({
'url' : url,
'type' : _type,
'dataType' : 'json',
'data' : data,
'cache' : false,
'success' : function(response) {
if (typeof callback != 'undefined' && callback) {
callback.call(_this, response);
return;
}
},
'error' : function(response) {
if (typeof errorCallback != 'undefined' && errorCallback) {
errorCallback.call(_this, response);
return;
}
}
});
};
You could use JSON object as data variable.
answered Jan 4 at 14:43
Ivan OvcharenkoIvan Ovcharenko
14211
14211
3
He doesn't use jQuery
– executable
Jan 4 at 14:43
1
"The better way is to use jQuery $.ajax()" - Why would including a 100kb bloated js library be a "better" way?
– Magnus Eriksson
Jan 4 at 14:44
Why is this "better"? Include an external dependency when it's not in use anywhere else?
– GrumpyCrouton
Jan 4 at 14:44
Hi thanks for your answer, I might come back to use jQuery.
– Paddy Hallihan
Jan 4 at 14:45
2
@PaddyHallihan - If you want to learn proper development, I would stick to what you got now and sort the issues out instead. You don't want to be a "library"-developer (a developer that doesn't actually knows the language, but only a few libraries). There's nothing wrong with using tools and libraries, but including a 100kb lib just for this is overkill and bad for the end user (that needs to download all that extra bloat).
– Magnus Eriksson
Jan 4 at 14:48
|
show 1 more comment
3
He doesn't use jQuery
– executable
Jan 4 at 14:43
1
"The better way is to use jQuery $.ajax()" - Why would including a 100kb bloated js library be a "better" way?
– Magnus Eriksson
Jan 4 at 14:44
Why is this "better"? Include an external dependency when it's not in use anywhere else?
– GrumpyCrouton
Jan 4 at 14:44
Hi thanks for your answer, I might come back to use jQuery.
– Paddy Hallihan
Jan 4 at 14:45
2
@PaddyHallihan - If you want to learn proper development, I would stick to what you got now and sort the issues out instead. You don't want to be a "library"-developer (a developer that doesn't actually knows the language, but only a few libraries). There's nothing wrong with using tools and libraries, but including a 100kb lib just for this is overkill and bad for the end user (that needs to download all that extra bloat).
– Magnus Eriksson
Jan 4 at 14:48
3
3
He doesn't use jQuery
– executable
Jan 4 at 14:43
He doesn't use jQuery
– executable
Jan 4 at 14:43
1
1
"The better way is to use jQuery $.ajax()" - Why would including a 100kb bloated js library be a "better" way?
– Magnus Eriksson
Jan 4 at 14:44
"The better way is to use jQuery $.ajax()" - Why would including a 100kb bloated js library be a "better" way?
– Magnus Eriksson
Jan 4 at 14:44
Why is this "better"? Include an external dependency when it's not in use anywhere else?
– GrumpyCrouton
Jan 4 at 14:44
Why is this "better"? Include an external dependency when it's not in use anywhere else?
– GrumpyCrouton
Jan 4 at 14:44
Hi thanks for your answer, I might come back to use jQuery.
– Paddy Hallihan
Jan 4 at 14:45
Hi thanks for your answer, I might come back to use jQuery.
– Paddy Hallihan
Jan 4 at 14:45
2
2
@PaddyHallihan - If you want to learn proper development, I would stick to what you got now and sort the issues out instead. You don't want to be a "library"-developer (a developer that doesn't actually knows the language, but only a few libraries). There's nothing wrong with using tools and libraries, but including a 100kb lib just for this is overkill and bad for the end user (that needs to download all that extra bloat).
– Magnus Eriksson
Jan 4 at 14:48
@PaddyHallihan - If you want to learn proper development, I would stick to what you got now and sort the issues out instead. You don't want to be a "library"-developer (a developer that doesn't actually knows the language, but only a few libraries). There's nothing wrong with using tools and libraries, but including a 100kb lib just for this is overkill and bad for the end user (that needs to download all that extra bloat).
– Magnus Eriksson
Jan 4 at 14:48
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54040970%2fpass-array-from-js-to-php-and-create-session-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you sure it gives you a string with comma separated values, or is it more like
["value_1", "value_2", "value_3"]
?– GrumpyCrouton
Jan 4 at 14:39
yeah I do
var_dump($_SESSION['values']);
and it returnsC:wamp64www<directory><file>:<line>:string 'value1,value2,value3,value4,value5
– Paddy Hallihan
Jan 4 at 14:43
You could stringify the array to a json string that you post to PHP, which you then decode in PHP.
– Magnus Eriksson
Jan 4 at 14:43
1
Possible duplicate of Sending Arrays via Ajax/JSON without JQuery
– executable
Jan 4 at 14:47
1
@executable I don't get your flagging as a possible duplicate and an answer. I feel the flag should be removed. That isn't fair for others later who might want to post more answers.
– Funk Forty Niner
Jan 4 at 15:52