Forum closed. New forum available at http://community.wymeditor.org/

Save data via ajax

Discuss features, code, contributions, ideas, suggestions, ...
For bugs, patches and feature requests, please post on the Trac:
http://trac.wymeditor.org/

Save data via ajax

Postby ckoehler on Thu Jun 07, 2007 1:45 am

Hello,

I have run into a problem with the editor. I am trying to save the text via ajax without reloading the page, so I have a onclick event on the submit button. Unfortunately the text isn't there when I hit that button, and wym.update() refreshes the page because wym cannot be found. I tried using the tip from another thread with var wym=aWYM_INSTANCES[0];, but that didn't work either.
Adding the class wymupdate to the submit button isn't an option since that requires reloading the page; all it does is call wym.update() anyway.

Any ideas?
Thanks!

Christoph
ckoehler
 
Posts: 13
Joined: Thu Jun 07, 2007 1:40 am

Postby misterm on Thu Jun 07, 2007 2:23 am

Just from the top of my head, the update() is simply a function, the submit will then be submitted by javascript. I'm almost certain you can rewrite it to do an XHR-call. Just dive into the main wym-editor.js

I think..
misterm
 
Posts: 14
Joined: Thu May 17, 2007 5:17 pm

Postby jfh on Thu Jun 07, 2007 8:06 pm

Hi Christoph, I think you need to use the postInit option.

Try something like this:
Code: Select all
postInit: function(wym) {

  $j('input.your-button-class').click( function() {
    wym.update(); //clean up the HTML and update the textarea value
    //do your AJAX call here
    return false; //disable the default behaviour
  });

}

You can also use another element (like an image) and bind the event handler on it, so the form doesn't get sent.
User avatar
jfh
Site Admin
 
Posts: 370
Joined: Sat Sep 23, 2006 8:43 pm
Location: Belgium

Postby ckoehler on Sun Jun 10, 2007 5:25 am

Thanks for the info! I haven't got a chance to play with that yet but will do so soon.

Christoph
ckoehler
 
Posts: 13
Joined: Thu Jun 07, 2007 1:40 am

Postby ckoehler on Mon Jun 11, 2007 11:56 pm

Works flawlessly, thank you!
ckoehler
 
Posts: 13
Joined: Thu Jun 07, 2007 1:40 am

Postby jfh on Tue Jun 12, 2007 9:41 am

Cool!

It'd be nice if you could post a code example with the AJAX call, so other users can learn how it works.
Thanks!
User avatar
jfh
Site Admin
 
Posts: 370
Joined: Sat Sep 23, 2006 8:43 pm
Location: Belgium

Postby ckoehler on Tue Jun 12, 2007 12:31 pm

And here it is!

Code: Select all
$(document).ready(function() {
     $.ajaxSetup( {
      url: "myurl.php",
      type: "POST"
   });
   
   $(".wymeditor").wymeditor({
   
      // fill text from the content div into the editor
      html: $("#userresume").html(),
      postInit: function(wym) {
        $j("#submit").click( function() {
         
          wym.update(); //clean up the HTML and update the textarea value

         // get text from the text area
         var text = $("#text").val();
         
         // make ajax call to server
         var html = $.ajax({
            data: "cmd=settextt&text="+text,
            success: function(msg) {
            
               // replace text from content div with updated text
               $("#userresume").html(text);
               
            },
         });

          return false; //disable the default behaviour
        });

      }
   });
});
ckoehler
 
Posts: 13
Joined: Thu Jun 07, 2007 1:40 am


Return to Developers

Who is online

Users browsing this forum: No registered users and 3 guests