0

How to insert String with new line on a header with php? Below are the codes that I did,

$error = "Not enough balance to submit. \nBalance available : " . $balance;
header('location:info-page.php?error=' . $error);

When I submit, it will give this error:

Warning: Header may not contain more than a single header, new line detected.

UPDATE CODES

$error = urlencode("Not enough balance to submit. \nBalance available : ". $baki_cuti);
header('location:permohonan_maklumat_penyelia.php?id='.$pid.'&error='. $error));

Page : permohonan_maklumat_penyelia.php

<?php
if (isset($_GET['error'])) {
    echo '<script type="text/javascript">
        alert("' .urldecode($_GET['error']) . '");
    </script>';
}
?>

Error received:

permohonan_maklumat_penyelia.php?id=842&error=Not+enough+balance+to+submit.+
Balance+available+%3A+0:112 Uncaught SyntaxError: Invalid or unexpected token
Amran
  • 537
  • 2
  • 7
  • 24
  • Your question might meet [an answer here](http://stackoverflow.com/questions/3871729/transmitting-newline-character-n) – alariva Nov 25 '16 at 02:25
  • 1
    Thanks for the reference @alariva. May I know how to apply it on the string? I tried to replace \n with %0A but the it will ouptut the %0A not as new line. – Amran Nov 25 '16 at 02:52
  • check wrapping your output with urldecode(): `echo urldecode($string);` and if you require also use htmlentities(): `echo htmlentities(urldecode($string));` The answer of @flauntster should help as well. – alariva Nov 25 '16 at 03:04

1 Answers1

2

You could use urlencode to encode whatever string you like to be appended to the URL. And the receiving script could use urldecode to decode that string.

eg:

$error = urlencode("Not enough balance to submit. \nBalance available : $balance");

Hope this helps :)

flauntster
  • 1,897
  • 11
  • 18