2

I've created a new array in javascript and I'm adding values to it indexes from a function an then passing the array to the ajaxCall function were I try to convert it to json and send it to a php file via ajax, but the variable json is allways empty. I've been reading a lot about how to send javascript objects json_encoded via ajax and looks like this is the way to do it, but obviously I haven't readed enought or there is something I've been missing. Anycase I'm newbie in javascript and any help would be apreciated.

    function createArray()
    {
        var advancedFormVars = new Array();
        advancedFormVars['checkbox1'] = document.getElementById('OfferID').value;  
        advancedFormVars['checkbox2'] =document.getElementById('offerName').value;

    AjaxCall(advancedFormVars);
    }
    function AjaxCall(advancedFormVars){
    var json = new Array();
    json = JSON.stringify(advancedFormVars); //in debuger it shows me this as content of json variable--> [] but advancedFormVars is not empty

$.ajax({
        url : 'AL_loadForm.php',
        type : 'POST',
        data : {
            json : json 
        },
        dataType:'json',
        success : function(data) {
            alert(data);
    }
...
4
  • 2
    An associative array in PHP would be an object in JavaScript. Change advancedFormVars to -> var advancedFormVars = {}; Commented Nov 17, 2015 at 16:59
  • you mean to replace var advancedFormVars = new Array(); to var advancedFormVars = {}; ?? Commented Nov 17, 2015 at 17:04
  • 1
    Yes. var json = new Array(); is not needed btw. as you're overwriting the array with the result of JSON.stringify() in the next line. Remove the line and add the var to the next line -> var json = JSON.stringify(...); Commented Nov 17, 2015 at 17:08
  • Thanks a lot Andreas you are the boss!!! Commented Nov 17, 2015 at 17:15

2 Answers 2

0

You are trying to use your array as a hash, so the values are not being set..

Instead of setting

var advancedFormVars = new Array();

Try setting

var advancedFormVars = {};

Example

JS:

var advancedFormVars = {};
advancedFormVars['checkbox1'] = 'valueA';
advancedFormVars['checkbox2'] = 'valueB';

var json = JSON.stringify(advancedFormVars);

console.log(json); //{"checkbox1":"valueA","checkbox2":"valueB"}

PHP

<?php
$json = '{"checkbox1":"valueA","checkbox2":"valueB"}';
$obj = json_decode($json);
var_dump($obj);
/*
    object(stdClass)#1 (2) {
      ["checkbox1"]=>
        string(6) "valueA"
      ["checkbox2"]=>
        string(6) "valueB"
    }
*/
?>
2
  • and the way of assignment is the same? will this work as well ---> advancedFormVars['checkbox1'] = document.getElementById('OfferID').value;?? Commented Nov 17, 2015 at 17:14
  • 1
    @user3033437 Yes. Have a look at Object and Working with object - (to prevent any confusion when reading the links^^: var foo = new Object() is the same as var foo = {}) Commented Nov 17, 2015 at 17:17
0

If all you have are two smaller arguments, I'd keep it simple and make an http get request. Encode your arguments if neccessary.

var url = "http://wherever.com/something.php?arg1=";
url += document.getElementById('OfferID').value;
url += "&arg2=" + document.getElementById('offerName').value;
httpGetAsync(url, returnMethod);

function httpGetAsync(theUrl, callback)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() { 
    if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
        callback(xmlHttp.responseText);
}
xmlHttp.open("GET", theUrl, true); // true for asynchronous 
xmlHttp.send(null);
}
1
  • nop, this is only for the example code, I have a lot of arguments, thats why I'm using an array Commented Nov 17, 2015 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.