I see quite a few different issues with the alert window and new lines. Most are that the \n
is considered a new line in PHP rather than getting sent to javascript.
In my case, the string is being outputted in a new window showing \n
. I just tried actually writing \n
into an alert box via jsfiddle, and that worked, so it must be my method of doing things...
Here is the string returned to console. as you can see, the \n is definitely there:
Username is required\nPassword is required\nEmail is required\nPhone is required\nCardnumber is required
However, it shows up like this:
Why is this happening? I think it may have something to do with the data type, as it is returned from $.ajax
if (canAjax && !try_ajax) {
e.preventDefault();
$.ajax({
type: "POST",
url: "mobilesubmit.php",
data: {"use_ajax": true, "formdata": $("#register_form").first().serializeArray()},
success: function(data) {
// This stupid thing should make new lines!
alert(data);
console.log(data);
},
error: function (request, status, error) {
try_ajax = true;
$("#register_form").submit();
}
});
}
The most likely cause of this is that your PHP code is escaping the backslash by adding another backslash in front of it. By the time it gets to your JS code, it's actually
"Username is required\\nPassword is required..."
You can inspect the raw response in the network panel of your debugger. If you try to view it in the console, it'll display the formatted output instead of the raw output.
Double-check your method of JSON serialization in your PHP code and make sure it's doing what you expect with the \n.
If your console log is showing \n
rather than a new line, then it means the \
is escaped in the original string...
console.log(1,"\\n\\n");
console.log(2,"\n\n");
use .replace()
to swap your \n
with newline characters
console.log(3,"\\n\\n".replace(/\\n/g,"\n"))
try adding a space after \n .It should work
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With