0

In JavaScript I have the following code:

 for (i = 1; i<3; i++) 

{

// above I collect check_a and check_b through radio button responses.

var connectJSON = ",";
var passObj = {id:i, ask:check_a, description:check_b};

// adding object string together in tempObj
var tempObj = tempObj + connectJSON + passObj;
}

// get ready for transport to server and display result of string
 var data = JSON.stringify(tempObj);
 console.info("info: " + data);


 // post string to server

  $.ajax
  ({
  type: 'POST',
  url: 'out.php',
  data: {data: data},

  success: function(msg)
  {
  alert(msg);
   }

});

In out.php I try to determine the result back from the server. The code is as follows:

<?php

if (ISSET($_POST['data']))

    {
    echo "TRUE";
    }
    ELSE
    {
    echo "False";
    }

var_dump($_POST['data']);

?>

I am getting this message, AJAX alert (msg) :

**True** string(42) ""question, [object Object], [object Object]""

Apparently this message is describing the string array being passed.

What I now need to do, if the format is correct, is to be able to access the string array - maybe with JSON_decode and identify properties of the array so that I can make insertions into a MySQL database.

Thanks for any AND all help...

1
  • connectJSON + passObj; This will force string conversion of passObj which would return [Object object]. Instead do ...+ JSON.stringify(passObj) Commented Dec 13, 2017 at 13:23

4 Answers 4

1
var connectJSON = ",";
var passObj = {id:i, ask:check_a, description:check_b};

// adding object string together in tempObj
var tempObj = tempObj + connectJSON + passObj;

First of all. If you just test this part in the console you will see that if you concatenate JS object and the string ',' you get the string "[object Object],[object Object]". You need to first stringify the JS object before concatenating it with strings.

Second I can really seem to understand your code but looping that code will just override those variables because they are declared in the loop so that doesn't seem correct. Probably you want to get the declarations out of the loop.

Otherwise it's kind of like this - you stringify the Js object and pass it as data to the ajax.

Sign up to request clarification or add additional context in comments.

Comments

0

No. To build JSON, first build a valid structure, then use JSON.stringify on the result; don't convert to string while building the structure. connectJSON + passObj will force passObj to string, resulting in "[object Object]".

Instead:

var array = []; // <== An empty array
for (i = 1; i<3; i++) 
{
    // Push entries into the array
    array.push({id:i, ask:check_a, description:check_b});
}

// Convert to JSON
var data = JSON.stringify(array);

Side note: The code in your question didn't declare i anywhere. If your real code doesn't, it's falling prey to The Horror of Implicit Globals.* Be sure to declare your variables. :-)


* (that's a post on my anemic little blog)

5 Comments

Fantastic for your help so far! I have managed to get the correct response from my php page which is listing an array. I believe that I am passing in AJAX a array string which I can print using $_POST. The problem I am having NOW is when I try to loop through the array to identify its contents as I need to make insertions into a database.
<?php if (ISSET($_POST['data'])) { echo "TRUE./n"; }ELSE{ echo "False./n"; } // look at array var_dump($_POST['data']); // identifies an array property or key / name pair var_dump($_POST['data'][1]["ask"]); // foreach loop: to try to identify array elements foreach($data as $user) { echo $user['ask']; } ?>
Notice: undefined variable data.
@Tunji: That would be a new question (if you can't figure it out first).
Thanks for your help
0

The issue is here var tempObj = tempObj + connectJSON + passObj;. You are concatinating objects and strings. In that case JavaScript will use Object.prototype.toString() first and then do the concatination. and Object.prototype.toString() in case of objects will produce [object Object]. To fix this you have to create an array like below.

var tempObj = [];
for (i = 1; i < 3; i++) {
  // above I collect check_a and check_b through radio button responses.
  var connectJSON = ",";
  var passObj = {
    id: i,
    ask: check_a,
    description: check_b
  };
  tempObj.push(passObj);
}

Also, you can skip JSON.stringify() and directly submit the JS object.

Comments

0

I'm trying to understand how is this deceleration : var tempObj = tempObj + etc...; possible ? you cant set the value of something you just declared, to the same thing you just declared .

Comments

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.