Answer the question
In order to leave comments, you need to log in
What is the error of the ajax_encode function in php and the output of characters \n\t\t\t\t?
Good day.
Faced the problem of output through the function ajax_encode()
. Instead of the expected result, it gives me a krakazyabra with characters like \n\t\t\t\t.
What am I doing wrong? What's wrong there? I tried to do var_dump nothing suspicious, I tried to track errors - nothing too. So what's the deal then?
This is what I expect to get.
And this is what I get with json_encode
output
This is a screenshot of the php
code This is the php code in text:
if($message['user_id'] == $loggedUser->id)
{
$messages_container[] = "<li class='media media-current-user m-b-md conversation_i_sender_block conversation_container_text'>
<div class='media-body'>
<div class='media-body-text conversation_i_sender_text conversation_container_text'>
<span class='pull-right delete_message fa fa-trash' data-dialog-id='".$message['dialog_id']."' data-message-id='".$message['id']."'></span>".$message['text'].$ifHasImg."
</div>
<div class='media-footer'>
<small class='text-muted'>
<a href='profile.php?user=".$user_items['username']."'>".$user_items['firstname']." ".$user_items['lastname']."</a> ".$message['day']." ".$pubMonth." ".$message['year']." в ".$message['hours'].":".$message['minutes']."
</small>
</div>
</div>
<a class='media-right' href='profile.php?user=".$user_items['username']."'>
<img class='img-circle media-object conversation_avatar' src='matroskin.jpg'>
</a>
</li>";
}
if($message['user_id'] == $with_user)
{
$messages_container[] = "<li class='media m-b-md conversation_i_receiver_block conversation_container_text'>
<a class='media-left' href='profile.php?user=".$user_items['username']."'>
<img class='img-circle media-object conversation_avatar' src='matroskin.jpg'>
</a>
<div class='media-body'>
<div class='media-body-text conversation_i_receiver_text'>
<span class='pull-right delete_message fa fa-trash' data-dialog-id='".$message['dialog_id']."' data-message-id='".$message['id']."'></span>".$message['text'].$ifHasImg."
</div>
<div class='media-footer'>
<small class='text-muted'>
<a href='profile.php?user=".$user_items['username']."'>".$user_items['firstname']." ".$user_items['lastname']."</a> ".$message['day']." ".$pubMonth." ".$message['year']." в ".$message['hours'].":".$message['minutes']."
</small>
</div>
</div>
</li>";
}
}
echo json_encode($messages_container);
$(".media-list-conversation").load("ajax/upload_message.php",{ action : true, talking_with : "<? echo $_GET['talking_with']; ?>" });
Answer the question
In order to leave comments, you need to log in
In general, the problem was solved by using the $.ajax();
Here's what the code looks like:
$.ajax({
type: "POST",
dataType: "json", // этот параметр добавил и проблему исправило...
url :"ajax/upload_message.php",
data : { action : true, talking_with : "<? echo $_GET['talking_with']; ?>" },
success : function(datas)
{
$(".media-list-conversation").html(datas);
}
});
\t is tab.
\n - transfer.
Find out where they come from. And outputting html code from PHP is bad practice. Use templates.
in json_encode try adding - "JSON_UNESCAPED_UNICODE"
php.ru/manual/function.json-encode.html
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question