-
Apr 2nd, 2012, 08:02 PM
#1
Thread Starter
Fanatic Member
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();
}
-
Apr 3rd, 2012, 12:05 AM
#2
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.
-
Apr 6th, 2012, 03:07 PM
#3
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.
-
Apr 6th, 2012, 06:09 PM
#4
Re: best AJAX method
Yes - use jQuery library instead
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|