1

I have a project that saves user inputs from HTML form and get saved in MySQL db.

For example, I have this function

function funcGetRecordDetails(editID){
    $('#hiddenID').val(editID);
    var action = "get_record_teams";

    $.ajax({
      url : "teams_post.php",
      type : 'post' ,
      data : { editID : editID,
               action: action } ,
      success : function(data, status){

        var edit_data = JSON.parse(data);

        $('#mdl_update_team_order').val(edit_data.order_no);
        $('#mdl_update_team_team').val(edit_data.team);
        $('#mdl_update_team_names').val(edit_data.name);

      }
    });

  }

In my case edit_data.name will have line breaks. But mdl_update_team_names textarea is NOT displaying the line breaks properly.

For example, the data in DB is saved as a\nb and that is exactly how it will be shown in the textarea.

For testing purpose, I changed it to $('#mdl_update_team_names').val('a\nb'); and the textarea IS displaying line breaks properly which is

a
b

Any idea how to display the line breaks properly from the DB?

Edit 1

When I do this, it works

var edit_data = JSON.parse('{"id":41,"snow_inc":"XXX","order_no":0,"team":"44","name":"a\\nb","update_by":"XXX","update_date_time":"2021-09-08 14:12:01"}')

But when I do this, it doesnt work.

var edit_data = JSON.parse(data)

But the response I'm getting is the same, which is

{"id":41,"snow_inc":"XXX","order_no":0,"team":"44","name":"a\\nb","update_by":"XXX","update_date_time":"2021-09-08 14:12:01"}

Tried this as well var edit_data = JSON.parse(data.toString()); with the same results.

  • I would start by using the JS debugger, in your browser's developer tools, to see if `edit_data.name` does indeed contain `a\nb`, at the line where you assign its value to the text area. – KIKO Software Sep 09 '21 at 10:37
  • check this: https://stackoverflow.com/questions/12729524/php-mysql-storing-line-breaks-in-text-area-in-database – capchuck Sep 09 '21 at 10:42
  • I'm not sure, but it worth a try, I found [an answer here](https://stackoverflow.com/questions/29574876/line-breaks-not-working-in-textarea-output), suggesting you could do: `.val(edit_data.name).replace(/\n/g, "
    ");` to replace `\n` by `
    `.
    – KIKO Software Sep 09 '21 at 10:43

0 Answers0