I am new to JSON format so please forgive my lack of knowledge. But somehow, I need to add some line breaks to my json_encode array so that it will parse with multiple lines. I've searched and searched but nothing fits my situation.
It's outputted as:
Following formats accepted: www.website.com website.com website.net/something
But I'm looking to output the data like this:
Website needs to be in the following formats:
www.website.com
website.com
website.net/something
I've tried:
echo json_encode(
array( 'status' => 'failed',
'message' => "Website needs to be in the following formats:\n
www.website.com\n
website.com\n
website.net/something"
), JSON_PRETTY_PRINT);
But Javascript is parsing it as a literal string so the newlines are ignored and outputted. Can I send the data from PHP to javascript using straight JSON format or do I have to use an array?
I've also tried using <br />
.
EDIT :
I am outputting the following way:
$.ajax({
url: "/forms/content_process.php",
type:'POST',
data: $(".content_cat").serialize(),
processData: false,
dataType: "json",
success: function(response) {
if (response.status == "failed") {
$(".content_status").css( "background-color", "red" );
$(".content_status").text(response.message).slideDown("normal");
} else {
$(".content_status").css( "background-color", "green" );
$(".content_status").text("Your category was submitted!").slideDown("normal");
}
}
});
use
\n
to insert newline character into the string. don't add a real newline as well.than on the client side before inserting it into your dom, you will need to replace the newlines with
<br />
or usewhite-space: pre
. alternatively you could surround each line with paragraph<p></p>
tags. to do so have a look here: jQuery text() and newlines.Looking back at this question, it became painfully obvious to me how simple the solution was. The reason I had such a difficulty, was because I needed to use
html
format.Of course it wasn't honoring
<br />
, I was formatting it as text! This was the simplest way to do it for my situation.