0

i want to call a php with jquery ajax to perform some database things and then return 2 lists of links. so i have to pass these two link lists back to jquery so it can display list 1 in the left side of the webpage and list 2 in the right side.

i created the lists in separate arrays that i send back to jquery with json_encode but i noticed that it escapes all the html characters.

<a>dog</a> became <a>dog<\/a>

so when i displayed the list in the html they werent links anymore.

how can i preserve the html codes in my returned arrays to jquery?

EDIT: is this the right way to go if you want to split up data from php so that jquery can display them in different locations in html?

// list 1
while($row = mysqli_fetch_assoc($saved_tags))
{
    $display_saved_tags[] = "<a id='showtag' href='answer.php?id=" . $row['id'] . "'>" . $row['name'] . "</a><br />";
}

// list 2
while($row = mysqli_fetch_assoc($related_tags))
{
    $display_related_tags[] = "<a id='showtag' href='answer.php?id=" . $row['id'] . "'>" . $row['name'] . "</a><br />";
}

// return lists to jquery
echo json_encode('display_saved_tags' => $display_saved_tags, 'display_related_tags' => $display_related_tags));
1
  • This is going to be a PHP thing. Can you post your actual PHP code so people can see how you're calling json_encode? Commented Dec 16, 2009 at 11:53

2 Answers 2

6

json_encode's escape characters directly conflict with HTML output. I have had the same issue but decided to use an alternative solution at the time. I just had a thought that you could perhaps do this:

$data = new stdClass();
$data->html1 = base64_encode('<h1>html in here</h1>');
$data->html2 = base64_encode('<p><strong>more html</strong></p>');
echo json_encode($data);

On the frontend:

callbackFunction(json) {
    alert(base64_decode(json.html1));
    alert(base64_decode(json.html2));
}

You would need the javascript implementations of base64_decode and utf8_decode which can be found at: http://phpjs.org/functions/base64_decode:357

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

2 Comments

i think its something wrong with the code in base64 and utf8. it says the functions are not declared when i use it. and when i debug the functions with netbeans it seems that the do{ } while is corrupt. removing it make the functions declared...do you got the code in your computer? dont know how to fix it..
it was nothing wrong with the code, it worked when i out it outside the jquery document ready function. thx it workes like a charm!
0

use can use below function to un-escape the chars when reading or sending on to the browser:

 html_entity_decode('your response here');

Also because your are using json_encode, make sure that you don't need the below function in your code:

json_decode

1 Comment

i used this in the php file: echo html_entity_decode(json_encode($display_saved_tags)); but its strange, it still escapes to <a>dog<\/a>. i can see it in firebug.

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.