-
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();
}
-
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.
-
Re: best AJAX method
+1 for the usage of jQuery for Ajax functionality.
-
Re: best AJAX method
Yes - use jQuery library instead