Results 1 to 7 of 7

Thread: Simple AJAX question

  1. #1
    Join Date
    Nov 2009

    Simple AJAX question

    I have a question regarding AJAX.

    I have a table with row lines. I want to delete table rows, when clicked in the first cell in the table. I use AJAX for this. Logic is following:

    1. Get the element (<td>) by id of the row, you want to delete. I use getElementByID(id) for this.
    2. Pass this id in an AJAX call to the server
    3. Delete the row in mysql table (php on the server)
    4. Return AJAX response and redraw table (omit already deleted row)

    This works with no problems for the first time. Problems start, if i want to delete second row .. The above procedure will fail, because the 'getElementByID()' function can not process data from the AJAX response, so the function fails with error something like "can not find ID" ..

    This phenomenon is explained here http://www.subbu.org/blog/2006/04/aj...getelementbyid

    I don't know however, how can I fix the problem. The site above mentions solution like "I was reminded that the solution is to use xml:id attribute in the XML so that getElementById can recognize ID type attributes", but this I don't understand.

    Does anybody know, how can I fix my problem? I am sure, I am missing something basic here.

  2. #2
    Join Date
    Nov 2002
    Consider the following example:

    HTML Code:
    <!DOCTYPE html 
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <script type="text/javascript">
    onload = function(){
    var el = document.createElement("div");
    el.id = "element";
    <a href="#" onclick="document.getElementById('element').innerHTML = 'Foo';">Do stuff</a>
    We can get elements that we add to the table using document.getElementById, so if you're re-drawing the table by appending everything that's returned by the server to the page it isn't an issue. I don't understand why you need to re-draw the table anyway, can't you just remove the row once it's been clicked?

  3. #3
    Join Date
    Nov 2009
    I don't understand why you need to re-draw the table anyway, can't you just remove the row once it's been clicked?
    Since I am a real javascript, ajax beginner, I just followed 'internet tutorials' where a whole table was sent back from the server in a reply. That's why

    Regarding your code:

    1. Does the 'onload' function simulates the AJAX call? Is it the same? If you sey 'yes', than i ask: "Are you sure?" Because I have tested dozen of times and getDocumentByID doesn't work for me, if data from AJAX reply are processed.

  4. #4
    Join Date
    Nov 2002
    You cannot use getElementById on an XMLDOM object but if you append the HTML nodes from an XMLDOM object to the page you can then access them using document.getElementById.
    If for some reason you need to access them before appending them to the page you could do:

    HTML Code:
    function getById(XMLDOMObject, tag, id){
    var elements = XMLDOMObject.getElementdByTagName(tag);
      for(var i=0; i<elements.length; i++){
        if(elements[i].id == id){
       return elements[i];
    return false;
    // usage
    var myElement = getById(request.responseXML, 'td', 'tableElement');

  5. #5
    Join Date
    Nov 2009
    You cannot use getElementById on an XMLDOM object but if you append the HTML nodes from an XMLDOM object to the page you can then access them using document.getElementById.
    This is how I get the result back to my page:

    var xmlhttp;
    function processUser(str, action, comment)
    if (xmlhttp==null)
      alert ("Browser does not support HTTP Request");
    var url="components/com_futbal/processuser.php";
    function stateChanged()
    if (xmlhttp.readyState==4)
    function GetXmlHttpObject()
    if (window.XMLHttpRequest)
      // code for IE7+, Firefox, Chrome, Opera, Safari
      return new XMLHttpRequest();
    if (window.ActiveXObject)
      // code for IE6, IE5
      return new ActiveXObject("Microsoft.XMLHTTP");
    return null;
    I am in the impression, that the line

    actually appends the result to my html page. Not? Or is there something else I need to do in order to append?

  6. #6
    Join Date
    Nov 2002
    No you need to use appendChild() to append to the document's DOM node structure as far as I am aware. I haven't got a great deal of time to look into it at the moment but you might be able to get away with:

    HTML Code:
    I think it all depends on the format of what is being returned from the server, if you are returning an entire HTML page with a doctype it can get quite tricky. I wrote a program a while ago that handled all of the ugly stuff, if the code above doesn't work I'll dig it out for you later on tonight.

  7. #7
    Join Date
    Nov 2009

    I think, you already helped me enough! I will do the tests .. At least, I know, where to proceed.

    I appreciate your time you've spent with me !! In case, i need to know more, I will come back to you.

    Thanks a LOT!


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.11550 seconds
  • Memory Usage 2,908KB
  • Queries Executed 15 (?)
More Information
Template Usage (35):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (2)bbcode_code
  • (3)bbcode_html
  • (2)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (7)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates