Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8126305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:06:25+00:00 2026-06-06T07:06:25+00:00

This is driving me crazy. I have a PHP script that gets some data

  • 0

This is driving me crazy.
I have a PHP script that gets some data from MySQL and saves it as a string with elements separated by commas.

A JavaScript takes that string and replace the commas by ‘\n’ and displays it inside a text area. Now, I can add new lines to the text from the text area. When clicking on a button, the string should be converted back to elements separated by commas, then set to the PHP (through AJAX) again, to be saved. The problem is with the JavaScript, that cannot detect the ‘\n’ character when there are more than 3 lines in the text-area.

// These are the lines of the text-area, after replacing ',' by '\n'
var textarealines=input_hidden_element.value.replace(',','\n').replace(',','\n');

// When trying to replace '\n' by ',' and send back, PROBLEM!
var AjaxRequest = WT.AjaxObject();
    AjaxRequest.open('POST', WT.AjaxLocation, true);
    AjaxRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    AjaxRequest.send('values=' + textarealines.replace('\n', ',').replace('\n', ','));
        AjaxRequest.onreadystatechange = function() {
            // Bla bla bla
        }

So, what am I doing wrong? Up to two lines the new lines are correctly replaced by commas but when I add more than 2 lines only the first 2 are replaced, next lines are sent with the ‘n’ character, as a single line of text containing ‘\n’. Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T07:06:27+00:00Added an answer on June 6, 2026 at 7:06 am

    Try adding the global search flag:

    textarealines.replace(/\n/g, ',')
    

    Note that the /\n/g is not in single quotes, meaning that it’s a regular expression.

    Similarly, to go from line feeds to commas, try:

    input_hidden_element.value.replace(/,/g, '\n')
    

    This question is similar to “How to replace all points in a string in JavaScript.”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is driving me crazy. I have Python 2.5 and MySQL-python-1.2.3 from the .exe
Ok, this is driving me crazy. I have a CA that needs to know
Any regex gurus around here? its driving me crazy. Say I have this string:
This is driving me crazy. I have the following string in a ASP.NET 2.0
This is driving me crazy. I have a library I source from multiple scripts,
Sorry for the stupid question, but this is driving me crazy... I have test_framework.php
This is driving me crazy. I have this one php file on a test
This is driving me crazy, I have a string in the format: <br />
This is driving me crazy. I have a very simple user control: public int?
This is driving me crazy and I can't find the answer anywhere. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.