0

So I have this on the PHP side:

//enclosed in for loop start
$titleArray[] = $title;
$idsArray[] = $id;
//enclosed in for loop end

After the loop runs, the arrays are populated, here is a var_dump showing that. And then we json encode it and exit, sending it as a response for jQuery to handle.

var_dump($titleArray);
array(2) {
  [0]=>
  string(2) "T1"
  [1]=>
  string(2) "T2"
}

var_dump($idsArray);
array(2) {
  [0]=>
  string(2) "I1"
  [1]=>
  string(2) "I2"
}

$output = array('success' => true, 'type' => 'valid', 'titles' => ''.$titleArray.'', 'ids' => ''.$idsArray.'');
echo json_encode($output);
exit();

Here is where I run into the issue. I can't seem to access those arrays (titles/ids) anymore. The response is:

{"type":"valid","titles":"Array","ids":"Array"}

Here is the post success area to handle the response:

success: function(output) {
             var x = $.parseJSON(output);
                 if(x.type=='valid'){
                     $.each(x.titles, function(k, v) {
                         $("#output-area").append('<div id="'+v.ids+'" class="title">Title: '+v.titles+'</div>');
                     });
                 }
         }

2 new divs should be appended at this point, as there are 2 items in the arrays. Instead, I get 5 newly appended divs with undefined as the v.ids and v.titles values. How can I get the correct and desired output in this scenario?

2 Answers 2

1

Basically you have pass Array to json_encode()

array('success' => true, 'type' => 'valid', 'titles' => $titleArray, 'ids' => $idsArray );
Sign up to request clarification or add additional context in comments.

1 Comment

By taking off the commas around the $titleArray & $idsArray, I now get 2 divs instead of 5, as expected. However, the values are still undefined in the outputted divs.
1

Create a single array

$data[] = array('id' => $id , 'title' => $title );
$output = array('success' => true, 'type' => 'valid', 'data' => $data );
echo json_encode($output);
exit();

in javascript

success: function(output) {
             var x = $.parseJSON(output);
                 if(x.type=='valid'){
                     $.each(x.data, function(k, v) {
                         $("#output-area").append('<div id="'+v.id+'" class="title">Title: '+v.title+'</div>');
                     });
                 }
         }

7 Comments

Really close! This fixes the undefined variable names, So far the only issue left is the output is all in one div like this <div id="T1,T2" class="title">Title: I1,I2</div> instead of outputting 2 unique divs
The jquery you posted in the comments does not work. Can you show what it would look like in your post on the PHP side? I am basically trying to remove the commas found here: <div id="T1,T2" class="title">Title: I1,I2</div> and creat a div for each.
var sep = ''; var ids =''; var titles=''; $.each(x.data, function(k, v) { ids += sep + v.id; titles += sep + v.title; sep = "," }); $("#output-area").append('<div id="'+ids+'" class="title">Title: '+titles+'</div>');
hmmm.. I still get the same exact output with the comma and 1 div instead of 2: <div id="T1,T2" class="title">Title: I1,I2</div>
\\Before Loop $sep =""; \\in loop ` $ids .= $sep . $id; $titles .= $sep . $title;` \\ display $output = array('success' => true, 'type' => 'valid', 'ids' => $ids,'titles' => $titles ); echo json_encode($output); exit(); \\and js ` success: function(output) { var x = $.parseJSON(output); if(x.type=='valid'){ $("#output-area").append('<div id="'+x.ids+'" class="title">Title: '+x.titles+'</div>'); } } `
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.