What is the meaning of carriage return in javascript-php context?

I enter data in the text box of my form, which contains 2 new line characters, but a carriage return character. (I can confirm strlen in js or use the str.charCodeAt () function.)

Now I submit this form to a php script that somehow gets a carriage return along with a new line character.

Basically, \nin js it becomes \r\nin php.

I have 2 questions:

  • Does anyone know why this might happen?
  • What is carriage return really used for?

Use case: checking the form on the server side and on the client side to limit the number of characters for the field.

+3
source share
2 answers

JavaScript treats newlines as \n, however, when submitting a form via HTTP, JavaScript has nothing to do with it (other than checking the text). Windows treats newlines as \r\n, so when you use Windows and press the return key in the text field, it actually enters \r\nthe field. When you send this data to the server, JavaScript does not do this (unless you use Ajax), your browser does this. Your browser will use the same parameter for newlines as your OS, and why you see them in your PHP file, but not in JavaScript. Different operating systems handle newlines differently. See here for more information.

Edit
. : " ", " " ( ) " ".

+4

\n \r\n. CR / , LF.

0

Source: https://habr.com/ru/post/1775272/


All Articles