Seiten-Inhalte inline bearbeiten
David Walsh hat mit Hilfe der MooTools die Möglichkeit geschaffen, Elemente einer Web-Seite direkt zu bearbeiten und zu speichern.
Ich finde die Idee sehr schön, die Umsetzung jedoch nicht ganz nach meinem Geschmack, darum habe ich eine konfigurierbare MooTools Klasse entwickelt, die wie folgt eingesetzt wird.
- Die Klasse EditableContent Im <head>-Bereich der HTML-Seite einbinden
- Im HTML die bearbeitbaren Elemente mit IDs versehen:
- Die Klasse konfigurieren und initialisieren:
- id: HTML-ID des bearbeitbaren Elements
- dbId: ID des Datensatzes in der Datenbank, der beim Speichern geändert werden soll
- targetUrl: URL des Scripts, das den geänderten Inhalt verarbeiten wird
- editMode: (’input’, ‘textarea’) Darstellung des Bearbeiten-Felds
save.php
save2.php
Fertig :). Eine Demonstration könnt ihr euch hier ansehen.
Feedback und Verbesserungsvorschläge sind willkommen!
Related Posts
*** YOU HAVE EITHER NOT INSTALLED SIMILAR POSTS PLUGIN OR IT IS DEACTIVATED. ***
AlexG | August 5th, 2008 at 8:22 am #
Hi Jerome!
Ihre Klasse hat gutes Potenzial. Könnten Sie mir sagen, wie Sie umsetzen würden dann der Fall, wenn Text ist leer wie David Walsh?
//get value, place it in original element
val = input.get(’value’).trim();
el.set(’text’,val).addClass(val != ” ? ” : ‘editable-empty’);
English: Your class has good potential. Could you tell me how you would implement the case where text is empty like David Walsh?
Regards,
Alex
Jérôme | August 5th, 2008 at 11:23 am #
@AlexG: thank you for your comment - I updated the class and the CSS file. If the editable content is empty, the surrounding element will keep a height of 1em (adjustable in the CSS).
Gianko | August 6th, 2008 at 7:09 am #
Hey Jerome!
great class!… I really like that you can see the changes when you update html tags.
I see some kind of error when using Opera and IE, it renders the tags in Uppercase, i.e. EM, STRONG, etc… thus making the site xhtml invalid.
Any way of fixing that?
Gianko | August 6th, 2008 at 9:18 am #
also, could you tell me how is the data passed via post.. wich variable do i use in the mysql query? the same as in David W.?
sorry if i’m annoying you :$
Jérôme | August 6th, 2008 at 11:12 am #
@Gianko: I just fixed an error in the class, until now, the dbId param has not been submitted (boooo!).
When you save an area, the class submits to POST parameters: *id* and *content*. So your PHP script has to take those two parameters to perform the save.
You should use something like this in your PHP script:
$id = intval($_POST['id']);
$content = trim(strip_tags($_POST['content']));
$query = “UPDATE mytable SET mycolumn = ‘”.mysql_real_escape_string($content)
.”‘ WHERE id = ‘.mysql_real_escape_string($id);
…
I hope it’s clear that you should add some additional security to the script.
I’ll update my post and add the backend sample code. Anyway you should redownload the JS class because of the corrected error.
Jérôme | August 6th, 2008 at 11:23 am #
I don’t know if I am understanding you correctly. I installed Opera and tested in IE7 and looked into the source code with the developer tools. Every tag was written in uppercase, even with JavaScript turned off, so I think this is probably not an error of the class.
And in the class itself I only use built in MooTools methods, so I just don’t know what I could do to fix the problems you have - I can’t reproduce them and I would not know how to fix them :).
You are not annoying at all :).
Alex Guidice | September 17th, 2008 at 12:33 am #
Hi Jérôme!
I’m continuing to use your wonderful class. Would you kindly post your “save2.php” and “save3.php” or just the lines that generate the error? I’m having trouble understanding how to feedback an “http” (no server found) error to alert users that their changes didn’t save.
Thanks!!!
Alex
Jérôme | September 17th, 2008 at 12:42 am #
Hi Alex,
thank you for your comment. I’ve upgraded the post and included the code of the files __save.php__ and __save2.php__.
The file __save3.php__ is simply not there, so the standard HTTP 404 error is returned.
If you have further questions, please feel free to ask :).
AlexG | September 18th, 2008 at 12:53 am #
Jérôme,
This explanation is very helpful, but I don’t get an error as a result of “save3.php” not being present on your example. I can write verification code in php to validate that the data coming in is the data received back out, but would rather just have a simple 404 response to return if it should be in the class already.
Thanks again! I’m a fan.
Alex
Jérôme | September 18th, 2008 at 3:02 am #
Alex,
I am not sure if I understood you correctly: did you mean that you don’t see an error alert box when you try to save the third content area in __my__ example or in __your__ application?
If it is the latter: does your server correctly serves an unavailable page with a 404 status code?
Woha, that makes me think of something! Please re-download the JavaScript-File or review the method `_saveContent` in the `onFailure` area. I have changed the code from
onFailure: function(instance) {
if (instance.status == 404){
alert(elmInfo.targetUrl + ‘: ‘ + instance.statusText);
}
}
to
onFailure: function(instance) {
alert(elmInfo.targetUrl + ‘: ‘ + instance.statusText + ‘ (’ + instance.status + ‘)’);
}
I suppose that my error has been that I explicitely checked the status 404 instead of just alerting the failure reason. Let’s face it: onFailure is onFailure, and there can be many reasons ;).
I hope this brings you a step further!
Best
- Jérôme
Dmitry | December 13th, 2008 at 1:59 pm #
It would be nice to have an ability to in-place edit lists also. You may pass an array of options in the config and then do setHTML accordingly.
It looks good - you may display your forms in a pretty compact way & edit one field at at time.
Cheers!