SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    problems with textarea once text is edited

    HI,

    This question involves AJAX but this is not an ajax problem as the ajax works...this is a DOM related question. IM sorry its such a long read but its the only was i could describe my problem

    I have an ajax app that allows a user to select a message from a list and then edit it in a textarea to the right of the list. Once the user has finished editing the message they click a submit button some more ajax runs and the message is sent back to the server. A confirmation message is the printed in place of the edited text in the textarea.

    Now all works fine up to a certain point....a user can click edit, the message is loaded in the textarea and the user can click submit and the edited message gets submitted to the server.

    The problem however is here. If the user clicks edit, clicks in the textarea but doesnt edit any text and then presses submit everything is fine and the confirmation message is printed. However id the user edits the message and then presses submit there is no confirmation printed and the user cannot click any further buttons....the page has to be reloaded for the app to do anything else.....BUT the message has been edited....

    so i dont think its an ajax problem, i think its more something to do with the DOM...has anyone ever encountered problems with textareas once they've been clicked in/text edited?

    I cant work this out....ive debugged and debugged but cant find the problem....ive also googled it do death....heres my code. First the functions ( sendmessage() sends the text and showconfirmation() prints the conf...duh!) and then the related html....

    Code JavaScript:
    function send_message(msg) {
        var message_id = msg.id;
       	var updated = document.getElementById("textbox");
       	var updated_txt = updated.TextNode;
       	var url = "edit_message.php";
       	request.open("POST", url, true);
       	request.onreadystatechange = show_confirmation;
       	request.send("message_id=" + escape(message_id) +
                       "message=" + escape(updated_txt));
       }
     
       function show_confirmation() { 
       alert('polo');
        if(request.readyState == 4) {
         var confirmation = request.responseText;
         var div = document.getElementById("textbox");
       	 var confirmation_area = div.TextNode;
       	 replaceText(confirmation_area, confirmation);
     
        }
       }

    Code HTML4Strict:
     <tr>
          <td><?php echo substr($message, 0, 30); ?></td><td><span style="color: red; font-weight: bold;"><?php echo $author; ?></span></td><td>
          <form>
     
          <input type="button" id="<?php echo $message_id; ?>" value="edit" onClick="get_message(this);" /></td>     </tr>
    <?php } ?>
      </table>
     <textarea style="width: 300px; height: 400px; border: 1px solid #ccc; margin-left: 20px;" id="textbox"></textarea>
      <input type="button" id="<?php echo $message_id; ?>" value="submit" onClick="send_message(this);" /></form>
     
     
        </div>

    Any ideas would be appreciated

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    on your readystatechanged event, you could alert or write the response made by the server, as show_confirmation is called more then once.

    I think an error is being thrown and because request.readystate does not equal 4, then your not seeing anything.


  3. #3
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Ok i'll give that a try....although i think the readystate is getting to four because i know the php script is working...it does update the text in the db.....so i doubt if the reponse is not being sent back....but ive not tried the avenue of alerting the server response

  4. #4
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Right ive put a print in where it says alert('polo'); in the above code ( in the show confirmation function) and indeed i do get the confirmation back ( i get two empty alerts for readystates 1 and 2 and then the confirmation text back for three and four)

    So i knew everything was working and it was, as i thought, a problem with getting the conf text in top the textarea i moved it outside...so the confirmation is shown in a p tag above the text area and guess what i get the confirmation!!!!

    Now its still not 100&#37; because after the user has edited and presses submit and got confirmation you cant click on any of the edit buttons...well you can but the text isnt loaded in to the textarea......

    anyways thanks for pointing me in the right direction

  5. #5
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yay it works! Sorted it....id post the solutions but it was a real combination of things...i spent 1 hour and a half debugging it on the train this morning.

    Ajax rocks....if you want to learn it i picked it up from a book called head first ajax......the book treats you like a kid, in that it tells you everything two or three times and gives you loads of big images/diagrams with speech bubbles and stuff like that. Its a little grating at points but it really gets the message through.....id managed to achieve some effects ive been wanting to do after the first two chapters!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •