1

I have a textarea in a html form where I put:

line1
line2
line3

Then I press button and send the text via jquery ajax to a php script that handles the ajax request and needs to enter each line to db table as a separate row.

Currently, I send the string as

encodeURIComponent($('#multiline_text').val()) in data: variable in jquery

then I'm trying to break the text into array using explode('\n', $multiline_text) in the php file and then enter each row to db table using foreach.

But I'm getting only one array element and that element is being entered as one row only into the db table and the row has value: "line1 line2 line3" without quotes, instead of having 3 separate rows in the table with values:

line1
line2
line3

What should I do?

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
Spec
  • 329
  • 2
  • 14
  • 2
    Text have to be prefixed with four spaces for a block of code. But, you **also have to prefix the block with an empty line**. If you want to mark inline code, enclose the phrase with backticks (`). – Rob W Jan 18 '12 at 21:39
  • An answer in this question http://stackoverflow.com/questions/1183466/new-line-problem-when-doing-ajax-post-with-jquery helped me to sovle this. Maybe someone has more elegant solution? – Spec Jan 18 '12 at 21:42
  • Try an ajax post instead. Maybe something with the encode is messing with it. – iLLin Jan 18 '12 at 21:43
  • @iLLin: I'm already using it: $.ajax({ type: 'POST', – Spec Jan 18 '12 at 21:44
  • 1
    Is this: `explode('\n', $multiline_text)` the exact code? If so, try using double quotes:`explode("\n", $multiline_text)` – bububaba Jan 18 '12 at 21:51

1 Answers1

3

Have you tried;

explode("\n", $multiline_text);

it will need double quotes to recognize the new line.

Jason Brumwell
  • 3,482
  • 24
  • 16