0

I'm working on a simple webpage for a company and the company wants to be able to edit the content themselves from time to time. However they have no programing knowledge and therefore I want to use an embedded HTML editor, I have chosen jQuery TE.

The problem is that I only know how to use this as a form, e.g.:

<form id = "wyForm" method="post" action="test.php">
    <textarea class="editor"name = "testText">Hi</textarea>
    <input type="submit" class="wymupdate" />
</form>

Then I would convert the textarea to an editor with jQuery:

<script> $('.editor').jqte() </script>

This makes it possible to send the result to a .php page that updates the database. However many times I don't want to use a textfield or a form, but just a simple object that I convert to an editor in the same way. But how do I save the change in that case?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Christian.H
  • 126
  • 8
  • Why don't you want to use a form? That seems like a logical way of doing this. If you want an action to execute on submit (i.e. firing another PHP file) then you should use a form. – CaribouCode Jul 31 '13 at 15:26
  • What would be the purpose of not using a form? You mean you wanna ajax submit it? Without it being a form element? – Kylie Jul 31 '13 at 15:26
  • I was planning on using or

    instead as I want the user to be able to toggle the edit-mode by a button. In that case, the user sees a text (e.g. in a span) and is then able to press "edit" (which transforms the span to a jQuerty TE field) to edit that span. The user can then save by using a save button. If it is a text field, the user can already before pressing the button edit the text, but have to press the button to use the jQuerty TE. This becomes very confusing and uggly

    – Christian.H Jul 31 '13 at 21:35

1 Answers1

0

Catch the form submit event and copy the content to a hidden field.

<form id = "wyForm" method="post" action="test.php">
    <div class="editor" name="testText">Hi</div>
    <input type="submit" class="wymupdate" />
    <input type="hidden" id="editorHiddenField" />
</form>

...

$('#wyForm').submit(function() {
   $('#editorHiddenField').val($('.editor').html());
});

You may need to use an API to get the content instead (I'm not familiar with the plugin), but the concept is sound.

Edit - If you don't want to use a form at all:

<div class="editor></div>
<button id="SaveButton">Save</button>

...

$(document).ready(function() {
    $('#SaveButton').click(function(e) {
        e.preventDefault();
        $.post('savepage.php', { data: $('.editor').html() }).done(function() { alert('saved!'); });
    });
});
Jason P
  • 26,984
  • 3
  • 31
  • 45
  • My main concern is that I do not want to use a form, but a or

    . Otherwise I agree that your method is valid, you might even on the submit execute a $.post to alter the database which is desirable in my case

    – Christian.H Jul 31 '13 at 21:42
  • Yes that actually helps alot! I was having problems with figuring out how to get the content, but I see you are using .html() and that works. However now I get the content like: &It;strong>Hello&it;/strong> instead of Hello, do you know why? Thanks alot! – Christian.H Jul 31 '13 at 21:59
  • It may have to do with how the plugin works. `html()` may not be what you're looking for. There has to be a way though, so I'd recommend checking the docs for examples on how to get the content. – Jason P Jul 31 '13 at 22:06
  • True. I also just realized that if you use .html(), changes made in the jQuerty TE field will not appear in those methods. It will still show the original message in the . However if you use .val() it works and it captures the changes instantly! Regarding the docs it doesn't provide you with any help, it is very minimalistic – Christian.H Jul 31 '13 at 22:10