Search code examples
ajaxjquery-forms-plugin

AJAX jQuery form submission with HTML in form


I'm not terribly experienced with AJAX and so am using some tools and attempting to integrate and customize them. First, this is a wordpress site using an AJAX form to submit a post. It all works great! Now, of course, I have to add a WYSIWYG editor to the mix and need to figure out how to get the html content to submit properly.

Here's the submit code:

jQuery("form.pfs").submit(function() {
  // vvv I ADDED THIS LINE!!! vvv
  jQuery('textarea#postcontent').val(jQuery('div.workzone iframe').contents().find('body').html());
  jQuery(this).ajaxSubmit({
    type: "POST",
    url: jQuery(this).attr('action'),
    dataType:'json',
    beforeSend: function() {
      jQuery('.pfs-post-form #post').val('posting...');
    },
    complete: function(request,textStatus,error) {
      data = jQuery.parseJSON(request.responseText);
      if (data && data.error) {
        jQuery('#pfs-alert').addClass('error').html('<p>'+data.error+'</p>').show();
        jQuery('.pfs-post-form #post').val('Post');
      } else {
        jQuery('form.pfs').reset();
        location.reload();
      }
    }
  });
  return false;
});

As you can see, I've added that first line where I take the content of the HTML in the WYSIWYG (http://elrte.org/) and set it as the value of what would have been the original textarea (#postcontent).

The AJAX works perfectly if there's an error, so it is only the "else" portion of the routine that is throwing the error:

Uncaught SyntaxError: Unexpected token < jquery.js:2
e.extend.parseJSON jquery.js:2
jQuery.submit.jQuery.ajaxSubmit.complete pfs-script.js:31
F

So as you see, I need to validate for malicious code (not part of this help request!!) but more importantly I just want to be able to accept HTML in my form posts. Ideas? I am not 100% sure I understand what is failing and where...


Solution

  • turns out there was a conflict with another Plugin. tracing out the request.responseText revealed it. This code works just fine.