6

I'm using this function, to submit form in the background, with custom messages. It works perfectly, except with textarea fields. I've read that the serialize function has problems with ex. linebreaks.

 $(function() {
      $("#comment_form").validate({    submitHandler: function(form) {
        $.post('/u/r/l/', $("#comment_form").serialize(),
 function(data) {
            $('#comment_container').html(data);
                });
            }
        });

The textarea is a markitup! editor area.

wintercounter
  • 7,500
  • 6
  • 32
  • 46
  • Please format your code to use newlines and tabulations :) – Alexander Bird Nov 06 '10 at 22:19
  • I've did, the editor ruined it. But thx ;) – wintercounter Nov 06 '10 at 22:20
  • Take a look here, it may help: http://api.jquery.com/serialize/#comment-67394779 – DaiYoukai Nov 06 '10 at 22:20
  • Just to make sure I'm not jumping to wrong conclusions: are you trying to have the markitup textarea (that the user himself is changing) be serialized into data to send to the server? – Alexander Bird Nov 06 '10 at 22:24
  • Yes, that is the point. Maybe the solution will be that Achonix has linked, but i'm only beginner in jquery, and can't implent that to my script :S. (I'm rather a XHTML+CSS, designer pro :P ) – wintercounter Nov 06 '10 at 22:32
  • Why are you using formSerialize instead of just serialize? – Liam Bailey Nov 06 '10 at 23:33
  • I assume that you gave the textarea a name attribute and put it inside the form? – thejh Nov 06 '10 at 23:34
  • Liam: I've mentioned, that was just a mistake in my post. thejh: Of course. Normal post of the form works ok... Just the ajaxed jquery one not. I'm using this script on others forms, where no texarea found, with success. – wintercounter Nov 06 '10 at 23:38

3 Answers3

2

As stated here: http://api.jquery.com/serialize/#comment-67394779

function keepLB (str) { 
  var reg=new RegExp("(%0A)", "g");
  return str.replace(reg,"%0D$1");
}

$(function() {
  $("#comment_form").validate({ submitHandler: function(form) {
    $.post('/u/r/l/', keepLB($("#comment_form").formSerialize()), function(data) {
      $('#comment_container').html(data);
    });
  }
});

If it doesn't work, manually urlencode the textarea data:

$(function() {
  $("#comment_form").validate({ submitHandler: function(form) {
    $.post('/u/r/l/', "textareadata="+escape($("#mytextarea").value), function(data) {
      $('#comment_container').html(data);
    });
  }
});

And if you also want to send other form contents (note: don't give the textarea a "name" here, just an id!):

$(function() {
  $("#comment_form").validate({ submitHandler: function(form) {
    $.post('/u/r/l/',
    $("#comment_form").formSerialize()+"&textareadata="+escape($("#mytextarea").value),
    function(data) {
      $('#comment_container').html(data);
    });
  }
});
thejh
  • 44,854
  • 16
  • 96
  • 107
  • Doesn't submit my comment at all :S – wintercounter Nov 06 '10 at 23:49
  • @wintercounter: Which one? The second one only sends the textareas content. – thejh Nov 06 '10 at 23:52
  • I have many hidden fields too generated by the CMS so that wouldn't be ok. If the textareas name is "comment", than this: should print the jQuery'ed request? – wintercounter Nov 06 '10 at 23:55
  • In this example, the data gets sent under the name "textareadata" and gets read from the element with the id "mytextarea". – thejh Nov 07 '10 at 00:01
  • In a few words: This is a Comment form. Just to imagine: there's the form, under that the comments. And all this is being reloaded, on the submission of the form. Now with the 3rd script i've got a data back: undefined It's from PHP or JavaScript? – wintercounter Nov 07 '10 at 00:18
  • I assume that jquery couldn't get the content from the textarea :S – wintercounter Nov 07 '10 at 00:21
  • What a lame am i :) There this before the POST: $(".comment_wrapper").html("Loading..."); so it deletes the full form -.- Sry guys, and thanks everything ; – wintercounter Nov 07 '10 at 00:28
0

Here main_post_txt is the id of html text area element which you are using and in jquery you can get easily its value by using

var post_text = $("#main_post_txt").serialize();  
Ankur
  • 5,086
  • 19
  • 37
  • 62
Mahendra Tyagi
  • 417
  • 4
  • 3
0

One thought (if standard usage of jQuery serialize isn't working) is that the markitup code is taking that textarea and do something fancy with it so that it doesn't even act like a textarea anymore. Is there some way in Markitup API to retrieve the data perhaps?

Alexander Bird
  • 38,679
  • 42
  • 124
  • 159