best AJAX method-VBForums
Results 1 to 4 of 4

Thread: best AJAX method

  1. #1

    Thread Starter
    Hyperactive Member kevin_sauerwald's Avatar
    Join Date
    Feb 2002
    Location
    outside Philly
    Posts
    488

    best AJAX method

    I'm new to web dvlp.. and I needed to make use of some ajax, so I searched around and found some youtube vid explaining it and I kinda got mine to work and this guy did it this way...

    Code:
    	function SendVotes(FormName) {
    	    //alert(FormName);
    	    var str = '';
    	    var elem = document.getElementById("frm" + FormName).elements;
    	    //alert(elem.length);
    	    for (var i = 0; i < elem.length; i++) {
    	        if (elem[i].type == "radio" || elem[i].type == "checkbox") {
    	            if (elem[i].checked == 1) {
    
    	                str += elem[i].value + "|";
    
    	            }
    	        }
    	    }
    	    new Ajax.Request("AddVotes.php", {
    	        method: 'post',
    	        postBody: 'VotesData=' + str,
    	        onFailure: showResponseBad,
    			onSuccess: showResponse
    	    });
    	    document.getElementById(FormName).disabled = true;
    	    //$("#td1").css('background', '#F2BCBD');
    
    	}
    now that I need to make another.. for the life of me I cant get it to work.. so I searched for more examples and now I dont see that most examples use
    this kinda code.. its all using the XMLHttpRequest (below), so it that the better way ? should I stop even trying to do the above ? I think the above
    would work to post data to that php page but I had issues getting responses back.. I just want to make sure I start learning the correct or
    better way... opinions ?

    Code:
    function showUser(str)
    {
    if (str=="")
      {
      document.getElementById("txtHint").innerHTML="";
      return;
      }
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","getuser.php?q="+str,true);
    xmlhttp.send();
    }

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,504

    Re: best AJAX method

    XMLHttpRequest is a native object in Javascript whereas Ajax.Request appears to belong to the Prototype framework. Any framework wrapper for Ajax functionality is fine to use if it's what you want (I usually use jQuery's); it's good to at least be aware of what's going on behind the wrapper though.

  3. #3
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: best AJAX method

    +1 for the usage of jQuery for Ajax functionality.
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    15,769

    Re: best AJAX method

    Yes - use jQuery library instead

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.