0

I want to read GDPR info from a file, then in PHP echo out a confirm box with the info, and I then set a cookie, using php setcookie. I have trouble getting it to work:

$message = file_get_contents("my_gdpr_text.txt");
echo ('<script>confirm("'.$message.'")</script>'); 

The confirm box just does not pop up at all (not even a blank box). If I set the message value to "Oscar" or anything, it all works, so the problem must be the answer from file_get_contents. I know it is read correctly, by testing with print_r. Is some text formatting needed? Grateful for answer.

Perry J
  • 355
  • 3
  • 16
  • Please share more details. It should not be too hard to check the generated markup for errors, or your browser's error console – Nico Haase Feb 09 '21 at 14:41
  • Well if your `my_gdpr_text.txt` has a double quote in it you're going to break the encapsulation. Please provide rendered code. PHP is simple and likely not the issue (unless `$message` is empty, or you are receiving a 500) – user3783243 Feb 09 '21 at 14:49

1 Answers1

0
echo ('<script>confirm("'.$message.'")</script>');

The problem with this is that as soon as your $message variable contains any double apostrophes " your JavaScript breaks For example

$message = 'Please click "OK" to confirm';
echo ('<script>confirm("'.$message.'")</script>');

Will become

<script>confirm("Please click "OK" to confirm")</script>

Which is of course invalid because of the mismatch of apostrophes.

You could do something like this.

$message = file_get_contents("my_gdpr_text.txt");
$message = json_encode($message);
echo ('<script>confirm('.$message.')</script>');

See also:

Also consider if you really need PHP:

Hendrik
  • 756
  • 6
  • 16
  • Thanks for answer. The file contains no double-quotes. It is a numbered list of restrictions about not violating an individual's racial or ethnic origin etc. However I don't know if it's confidential so dare not put it out here. – Perry J Feb 09 '21 at 15:11
  • And no, json_encode makes no difference... :\ – Perry J Feb 09 '21 at 15:12
  • @PerryJ We don't need real file contents, lorem ipsum could be used, just include any non word characters that are in real file, ms quotes, non latin chars, etc. – user3783243 Feb 09 '21 at 15:16
  • @PerryJ you could also start debugging it by first creating a txt file that has only one word in it, to get minimal working solution. After that start adding parts from original file, to see where it breaks. – Hendrik Feb 09 '21 at 15:32
  • I have figured out there appears to be some limit of maximum 60 characters allowed. It all works if I reduce the file to: 'Please be advised that care should be taken when entering a' But: 'Please be advised that care should be taken when entering any' - does not work. – Perry J Feb 09 '21 at 15:39
  • @PerryJ Probably best off using a modal in that case. – user3783243 Feb 09 '21 at 16:09
  • Likely you have to encode your text file with linux EOL, see what does \n and \r\n – NVRM Feb 09 '21 at 23:09