1

I need to display a document (like in a Google Docs style view) and provide the ability for a user to make comments on that document.

Here's how it needs to work:

  • When the user views the document, they can use their mouse to highlight any amount of text they want (as they would within any other webpage)
  • Upon release of the highlight, some sort of modal dialog box appears asking them what they would like to comment on that portion of the text they selected
  • After clicking save in the modal box, their comment is saved in a MySQL database along with information about the section of the text they highlighted
  • Another user can view this document and see their comments on the different parts of the text.

I don't know how to tell the database what section of the text is highlighted so it can reference that and show the comment on that section of the text.

JJJ
  • 32,902
  • 20
  • 89
  • 102
Ben
  • 19
  • 4

3 Answers3

1

In Javascript:

var selectedText;
selectedText = window.getSelection();
Lil' Bits
  • 898
  • 2
  • 9
  • 24
  • `window.getSelection()`, not `window.getSelection`. Also, this is not enough to be helpful on its own. – Tim Down Apr 02 '12 at 00:21
1

Not sure where you're getting stuck at but the basic idea would be:

  1. Get the selected text using Javascript (see http://www.codetoad.com/javascript_get_selected_text.asp for an example)

  2. Submit that text to a PHP script using either an HTML form or an AJAX approach (AJAX would probably be what you're looking for based on the description of your app).

  3. Take the text passed to the PHP script and insert it into the db.

  4. Party like a rockstar.

TheOx
  • 2,208
  • 25
  • 28
  • Not so much that I'm stuck, just didn't even know where to begin on this one. What if someone was to highlight something that appears more than once in the document? How would I go about making sure the correct position in the document is saved? – Ben Apr 01 '12 at 20:15
  • 1
    You'll probably want to look at Ranges in Javascript - http://www.quirksmode.org/dom/range_intro.html – TheOx Apr 01 '12 at 20:20
0

I'd suggest some kind of character-index-based approach. I've answered a similar question recently here: execCommand insertHTML breaks stored window.getSelection()

Community
  • 1
  • 1
Tim Down
  • 318,141
  • 75
  • 454
  • 536