-1

I have a piece of code that I am not sure is operating the way I think it is -

echo json_encode(array('html' => $html_string, 'cid' => $cid));

My understanding of this is that it is taking the $html_string and the $cid and adding a Key to each with the line of 'html' => $html_string yes?

Then it is gathering both of these variables with the keys and packaging them into a single array, yes?

If that is the case, I am baffled as to why when I look at the output of these on the AJAX data return - dataType: 'JSON', success: function(data) { console.log(data.cid); that they do not contain a key. An example of a console log:

(3) […]
0: "45"
1: "46"
2: "47"
length: 3

Should not each contain the key cid?

user14955679
  • 181
  • 8
  • You're logging the value of `data.cid` so no, it won't include the key `cid` because you're already accessing that property – Phil Jan 26 '21 at 23:31
  • I see so how would I make that change on the input side so that the key is moving with it since not having that key is causing unidentified index issues later on down the line? If I log just `cid` instead of `data.cid` it tells me its undefined – user14955679 Jan 26 '21 at 23:32
  • Perhaps you meant to log `data`, eg `console.log(data)`. Then you will see both `cid` and `html` keys – Phil Jan 26 '21 at 23:34
  • I can log that and do see that but if I pass only data instead of `html.data` then I get nothing. Here is the further down the line question and problem with the complete code. Does the issue lie in how then everything is being transmitted before being encoded? https://stackoverflow.com/questions/65909464/passing-variable-through-ajax-to-php-function-getting-undefined-vairable-but-not – user14955679 Jan 26 '21 at 23:38
  • I honestly don't know what you're asking and questions on StackOverflow should include everything required within the question itself. This is sounding a lot like an [XY Problem](https://xyproblem.info/) – Phil Jan 26 '21 at 23:41
  • It is an XY problem, no doubt. but because I am havent been able to get an answer to the larger question, I keep trying to boil it further down thinking the question is too complex. That is why I wanted to check my understanding of this above before going on elsewhere. I am attempting to do my best, based on my understanding, as to solve my own problem. or one change creates another issue - I know that is programming – user14955679 Jan 26 '21 at 23:43
  • In your JavaScript code, `data` is an object with `html` (string) and `cid` (array) properties which you would access as `data.html` and `data.cid` respectively. After that, I'm really not sure what is or is not working for you as you have not included that code nor fully described the problems you're having with it – Phil Jan 26 '21 at 23:46
  • Thank you. That helps. And that answers this question. If you would like, I can swap the answer to you if you post it as an answer. The other is a larger issue but I will rephrase the other question to account for the help yo provide here. Maybe it will unlock something – user14955679 Jan 26 '21 at 23:48

1 Answers1

0

Double-check your AJAX call.

I don't know the values of $cid nor $html_string, but substituting values in, this is what should be happening.

$cid = 1;
$html_string = '<p>lorem ipsum</p>';
$data = json_encode( array('html' => $html_string, 'cid' => $cid) );
echo $data;

output:

{"html":"<p>lorem ipsum<\/p>","cid":1}
zelarian
  • 121
  • 1
  • 4
  • Thanks I am not getting the "html" in front of it. If youre looking for the whole code I have a second question that is the issue later down the line - https://stackoverflow.com/questions/65909464/passing-variable-through-ajax-to-php-function-getting-undefined-vairable-but-not – user14955679 Jan 26 '21 at 23:35