2

I am showing a popup window using ajax after calling my php file whenever I am able to write successfully in a file.

I am showing popup using below code where I use alert.

<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(function() {
    $('form').submit(function(e) {
        e.preventDefault();
        $.post({
            url: 'savefile.php',
            data: $(this).serialize(),
        }).done(response => {
            console.log(response);
            response = JSON.parse(response);
            if(response.message) {
                alert(response.message);
            }
        });
    });
});
</script>

The json I am returning is this:

echo json_encode([
    'success' => true,
    'message' => $ret . ' bytes saved to file. \n All data saved.',
]);

I wanted to show on popup as shown below where All data saved. goes in new line instead of getting shown in the same line. But when I try above code it is just escaping it instead of showing in new line. Is there any clean way to do this?

10 bytes saved to file.
All data saved.
David Todd
  • 63
  • 5

1 Answers1

2

PHP interprets backslashes literally when expressed in single quotes.

Instead, use doubles and the newline will be parsed and honoured.

'message' => $ret . " bytes saved to file. \n All data saved."
Mitya
  • 33,629
  • 9
  • 60
  • 107