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 7007055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:33:28+00:00 2026-05-27T21:33:28+00:00

The question is: How to save user input linebreak to database. (jQuery/php/mysql) I have

  • 0

The question is:
How to save user input linebreak to database. (jQuery/php/mysql)

I have a paragraph which is editable by the user.

<p contenteditable="true" id="forbiddenField">Text</p>

When I retrieve data from db with this:

<p contenteditable="true" id="forbiddenField"><?php echo nl2br($forbidden); ?></p>

and that works great.

onblur (.blur) will activate a jQuery script which will send the new edited data to a database.

$("#forbiddenField").blur(function(){
        var whatIsNowForbidden = encodeURI($("#forbiddenField").text());

        $.ajax({
            type: "POST",  
            url: "updateForbidden.php",  
            data: "forbiddenStuff="+ whatIsNowForbidden,
            success: function(){
            }  
        });
});

Problem:
If the user inserts new data, with linebreaks, then the linebreaks are not added to the data in the database, only the content is added.

I tried this among other things:
var whatIsNowForbidden = encodeURI($(“#forbiddenField”).text());

I looked at theese places for answers:
New Line in Textarea to be converted to <br/>
http://www.w3schools.com/tags/ref_urlencode.asp
Is replacing a line break UTF-8 safe?
how to escape input but save unescaped into the database
How to recognize every linebreak?

I can’t get it to work.
All input and hints are much appreciated!

  • 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-05-27T21:33:29+00:00Added an answer on May 27, 2026 at 9:33 pm

    Try removing the encodeURI() function from your javascript, like so:

    var whatIsNowForbidden = $("#forbiddenField").text();
    

    I made a fiddle to test this and javascript’s encodeURI() function doesn’t seem to translate the new lines made in the paragraph to %0A, like we were expecting to.

    By removing the function encodeURI() PHP should be able to save the text, including new lines, to the database. And then, PHP’s nl2br() should transform the new lines into <br /> tag’s with no problem.

    The fiddle is available here: http://jsfiddle.net/LN7QC/

    Final answer: use <textarea>

    The jQuery:

    $("#forbiddenField").blur(function(){
            var whatIsNowForbidden = $("#forbiddenField").val();
    
            $.ajax({
                type: "POST",  
                url: "updateForbidden.php",  
                data: "forbiddenStuff="+ whatIsNowForbidden,
                success: function(){
                }  
            });
    });
    

    adding to database:

    <?php
        include('connect.php');
    
                if($_POST['forbiddenStuff']){
                    $forbiddenInput = $_POST['forbiddenStuff'];
                }
    
    
        If($forbiddenInput){
            mysql_query("SET NAMES 'utf8'");
            $changeDataInDB = "UPDATE heroes_and_helpers SET forbidden='$forbiddenInput' WHERE name ='sabina'";
            mysql_query($changeDataInDB);
    
        }
    ?>
    

    fetching from database:

       mysql_query("SET NAMES 'utf8'");
        $fetchForbidden = mysql_query("SELECT * FROM heroes_and_helpers WHERE name='sabina'");
    
            if (!$fetchForbidden)
              {
              die('Ngt ar fel: ' . mysql_error());
              }
    
        $whatIsForbidden = mysql_fetch_array($fetchForbidden); 
        $forbidden = $whatIsForbidden['forbidden'];
    

    on the site:

    <textarea id="forbiddenField" style="width: 450px; height: 500px;"><?php echo $forbidden; ?></textarea>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Activity which lets the user input names and the save them.
I have the following question about JPA: Can I save the order of the
Image Dummy = Image.FromFile(image.png); Dummy.Save(image.bmp, ImageFormat.Bmp); what the question says i have these using
I have written a user control that captures some user input and has a
I am using HTML textarea for user to input some data and save that
I have a form that the user can save and return to edit. I
I have a form that the user can save and return to edit. I
I have a form that the user can save and return to edit. I
I have prepared a simple test case for my question. Just save it to
I have a template where the user can input data about himself, and upload

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.