0

My PHP script is sending back an array:

$currentStatus = ( isset( $status ) ) ? "1" : "0";
$html = "<tr><td>" . $email . "</td><td>" . ( ( $status->type == 0 ) ? "View Only" : ( ( $status->type == 1 ) ? "View & Mark" : "View, Mark & Edit" ) ) . "</td><td>Invited</td></tr>";

echo json_encode( array( $html, $currentStatus ) );

Then my jquery is appending this to the table:

...
success: function( result ) {

   var resultArray = eval( result );

   $( "#myTable tr:last").append( resultArray[0] );
   ...
}

Problem is, when it prints, it prints the following extra marks:

name@email.com<\/td>    View Only<\/td> Invited<\/td><\/tr>","1"]

If I only echo the HTML it appends to table just fine, but I cant do that because I need the $currentStatus back too to do something with it.

Thanks!

TimNguyenBSM
  • 817
  • 2
  • 15
  • 33

2 Answers2

1

Allowing \/ helps when embedding JSON in a <script> tag, which doesn't allow </ inside strings. JSON: why are forward slashes escaped?

You can set options to json_encode:

echo json_encode(array( ('<tr><td>test@test.com</td><td>Invited</td></tr>')), JSON_UNESCAPED_SLASHES| JSON_HEX_TAG);

JSON_UNESCAPED_SLASHES - available from 5.4.0
JSON_HEX_TAG - from 5.3.0
Community
  • 1
  • 1
Alexander
  • 807
  • 5
  • 10
0

I've played with the result that you add in your comment in the console. My code looks like this and it seems to work:

var resultArray = ["<tr><td>name@email.com<\/td><td>View Only<\/td><td>Invited<\/td><\/tr>","1"];
var resultStringify = JSON.stringify(resultArray);
var resultParse = JSON.parse(resultStringify);

resultParse //Result: ["<tr><td>name@email.com</td><td>View Only</td><td>Invited</td></tr>", "1"]

Trying to figure out why it work by first stringify it and then parse it.

Alexnho
  • 136
  • 3