Hello.

In general, I had such a problem, when transferring data through php via ajax, they already come in recoded form.

I warn you in advance that all files are in my UTF-8 encoding!

Code about the following:

// javascript $.ajax({ type: "POST", url: "/comment/ajax/setContent/" + commentElement.id + "/" + value, success: function (reply) { console.log(reply); commentElement.querySelector(".comment-text").innerHTML = ""; commentElement.querySelector(".comment-text").innerHTML = reply; close(); } }); 

 // php public function actionAjaxSetCommentContent($commentId, $content) { echo $content; Comment::updateComment($commentId, $content); return true; } 

As a result, if in JavaScript (e) I pass the string < p>Привет!< /p> then already in PHP, when I get this string through the $content variable, it turns into

% 3Cp% 3E% D0% 9F% D1% 80% D0% B8% D0% B2% D0% B5% D1% 82% 3C


Please help either to understand why such a transition occurs , or tell me what kind of encoding it is and how to overtake it on the php side back to normal!

  • echo rawurldecode('%3Cp%3E%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82%3C'); -> <p>Привет< - Visman
  • Yes it works thanks. That's just how you can make it so that he recoded the string completely and did not chop it off after a slash </ p> - Arhitector
  • So you are not properly transmitting data. url is used to specify the address where we send, and for the data is data api.jquery.com/jquery.ajax/#entry-examples - Visman
  • you don’t understand a bit ... I just work with the mvc pattern there, redirecting to different pages happens via the 'comment / ajax / getContent / ([0-9] +)' => 'comment / ajaxGetCommentContent / $ 1' routes, 'comment / ajax / setContent / ([0-9] +) / (. +) '=>' comment / ajaxSetCommentContent / $ 1 / $ 2 ',' catalog '=>' catalog / index 'for this and not specific page Arhitector
  • one
    I look at your js code and I don’t see the data transfer through the data element, but I see the value variable. From here I think that you are trying to transmit the data through the address bar :) - Visman

1 answer 1

You use the POST method, and pass the data in the GET parameters.
Try this:

 $.post("/comment/ajax/setContent/","id="+commentElement.id+"&value="+value, function(reply){ commentElement.querySelector(".comment-text").innerHTML = ""; commentElement.querySelector(".comment-text").innerHTML = reply; close(); }); 

and in php already simply reach out from the array $_POST['id'] , $_POST['value']