0

I have a piece of javascript that accepts text from a textbox and after writing it to the database prepends it to a list of previous items.

$(document).on('click', '.add_file_ins6a', function(){

    var job_idx=gl_job_idx;
    var ins_content=$('.add_instructions4c').val();

    var id=$('.useri').val();
    if(ins_content!="")
    {
        //write new instructions to database
        var id=$('.useri').val();
        var data="job_id="+job_idx+"&ins="+ins_content+"&client_id="+id+"&key=2";
        //alert(data);

        $.ajax({
                type:"POST",
                url:"admin_includes/get_instructions.php",
            data:data,
            success:function(html2){
                $('.add_instructions4').val("");
                var split_return=html2.split("|");
                var apply="";
                apply+="<div class='ins_55'>";
                apply+="<p><strong>Date</strong>"+split_return[1]+"</p>";
                apply+="<p>"+split_return[0]+"</p>";
                apply+="</div>";
                $(apply).prependTo('.ins_holder');
            }
        });//end ajax

    }
    else
    {
        alert("FAILED");
    }
    return false;
});

This works ok but if the user enters a newline in the textbox this displays a '\n' in the prepended listing. When the page is reloaded the '\n' is removed but no newline is created - this could be to do with the

tags or should I be using the tags?

Currently the data is being inputted in the textbox - sent to the server -written to the database and then sent back to the page before being prepended. Should I split the text data where there is a '\n' and add the result to

tags.

Not sure what the best practice here - glad for any opinions.

3
  • I think you are looking for nl2br() Commented Dec 11, 2012 at 9:26
  • When you say "textbox", do you mean a textarea or a text input? Can you post the code you are using to write out the textarea. Also, when you get the value from the server, does it still have the \n if you output it to the console? Commented Dec 11, 2012 at 9:27
  • textarea - sorry - too early in the morning :) - will try nl2br though Commented Dec 11, 2012 at 9:33

1 Answer 1

2

That could be done in javascript also, for example:

split_return[1].replace(/\n/g, '<br />')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.