Reputation: 8178
Have posting working wonderfully, and reading the response happily EXCEPT when one of the data elements has new lines in it (\n). Hours and hours of googling, and I'm completely confused and baffled at this point. What am I missing, or doing wrong with this to be able to receive a data element that has newlines in it? In this particular case I'm going to display it to the user, so
tags are fine, but I'd originally wanted it to be in a for editing and any newlines returned give me errors.
.click(function() {
var $form = $("#product_data_form");
var dataString = $form.serialize()
$.ajax({
type: "POST",
url: "utility02a.php",
data: dataString,
dataType: "json",
success: function(server_response)
{
$("div#my_results").append(server_response.MyTextWithNewLines);
}
});
Then, on the server, in PHP (and this works as expected unless there are \n's in the response
echo '{';
echo '"status":"'.$status.'",';
echo '"MyTextWithNewLines":"'.json_encode($sometext).'"';
echo '}';
I'm sure it's an encoding issue somewhere along the line, but my eyes are spinning trying to understand what to encode how, where and when.
In short, the data returned should be able to be in a textarea for editing, with the new lines properly represented in the input area for the user.
Upvotes: 0
Views: 235
Reputation: 1959
You may want to use json_encode
instead, as I use that with newlines with no issue. Build an associative array of the data you want to encode. The keys of the array will be the json objects members once it's decoded on the client side:
<?php
$data = new Array( 'status' => $status,
'mytextwithnewlines' => $sometext
);
echo json_encode($data);
?>
That should get it for you!
Upvotes: 1