Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
40
rated 0 times [  43] [ 3]  / answers: 1 / hits: 30078  / 10 Years ago, sat, august 30, 2014, 12:00:00

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.comn
website.comn
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);
}
}
});

More From » php

 Answers
46

use n to insert newline character into the string. don't add a real newline as well.



echo json_encode( 
array( 'status' => 'failed',
'message' => Website needs to be in the followingnformats:nwww.website.comnwebsite.comnwebsite.net/something
), JSON_PRETTY_PRINT);


than on the client side before inserting it into your dom, you will need to replace the newlines with <br /> or use white-space: pre. alternatively you could surround each line with paragraph <p></p> tags. to do so have a look here: jQuery text() and newlines.


[#69607] Wednesday, August 27, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sophiak

Total Points: 242
Total Questions: 90
Total Answers: 103

Location: Liechtenstein
Member since Wed, Dec 8, 2021
3 Years ago
sophiak questions
;