S
S
Sergey Maley2016-01-07 01:02:20
PHP
Sergey Maley, 2016-01-07 01:02:20

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.
da64af3c4c0443d5a031e34b54efad06.png

And this is what I get with json_encode
7af6898bd9fd451cb554cc43ab7600d9.png

output This is a screenshot of the php
1fe10c24c2254690aa1f1e5ccc386a5e.png

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);


Maybe the problem is in the jquery code?
I select this data through the load()
function Here is the code below:

$(".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

4 answer(s)
S
Sergey Maley, 2016-01-07
@spesher

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);
    }
});

Already working as it should.
Thanks everyone.

A
Artur Atnagulov, 2016-01-07
@atnartur

\t is tab.
\n - transfer.
Find out where they come from. And outputting html code from PHP is bad practice. Use templates.

P
profesor08, 2016-01-07
@profesor08

Look at the data you get from the
ajax/upload_message.php link

C
coderisimo, 2016-01-07
@coderisimo

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 question

Ask a Question

731 491 924 answers to any question