Forum Moderators: open

Message Too Old, No Replies

AJAX does not work without an alert

         

knkk

2:19 pm on Dec 21, 2008 (gmt 0)

10+ Year Member



Instead of an id getting its innerHTML changed, the entire page is getting refreshed with this function of mine (you may want to look just at the end of the function where there's an alert):

function morerating(ratingform, checkflag, loc)
{
var f = document.forms[ratingform];
var rating_done_flag = 0;
var params = "";
if (checkflag == true)
check = checkrating(ratingform);
else
check = true;
if(check == true)
{
f.submit_rating_done.value = "";
for (i=0;i < f.elements.length;i++) {
if(f.elements[i].type == 'radio' ¦¦ f.elements[i].type == 'checkbox')
{
if(f.elements[i].checked)
{
params = params + f.elements[i].name +'='+ encodeURI(f.elements[i].value) + '&';
if (f.elements[i].name == "rating_1" ¦¦ f.elements[i].name == "rating_2" ¦¦ f.elements[i].name == "rating_3" ¦¦ f.elements[i].name == "rating_4" ¦¦ f.elements[i].name == "rating_overall")
rating_done_flag = 1;
}
}
else if (f.elements[i].name && f.elements[i].value)
{
params = params + f.elements[i].name +'='+ encodeURI(f.elements[i].value) + '&';
}
}
shows = loc;
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
if(document.getElementById('morehide'))
{
document.getElementById('morehide').style.display = "none";
document.getElementById('morehide').innerHTML = "";
}
if(shows == "rating_main_block" ¦¦ shows == "rating_main_block_editorial" ¦¦ shows == "loginbox" ¦¦ shows == "editorial_loginbox")
{
params = params + "submit_rating_done=1&";
params = params + "xmlrequest=1&";
}
else
{
if(!document.getElementById('morehide'))
{
f.submit_rating_done.value = "Submit";
}
}
params = params + "shows="+shows;
var url='/blocks/profiles/common/user_store.php';
url=url + "?" + params;
xmlHttp.onreadystatechange=stateChangedALL;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
//setTimeout('window.scrollBy(0, 350)', 1000);
//alert('help2');
return true;
}
else
{
return false;
}
}

That alert('help2'), if removed, causes the page to just reload, but with it, AJAX works fine. I tried some dummy setTimeout option also as you can see, but that doesn't seem to help either. I also tried setTimeout("alert('help2')", 1000).

If it helps, these are the other functions referred to there, though they're pretty standard AJAX stuff:


function stateChangedALL()
{
if (xmlHttp.readyState==4 ¦¦ xmlHttp.readyState=="complete")
{
document.getElementById(shows).style.display="block"
if (xmlHttp.responseText != "")
document.getElementById(shows).innerHTML=xmlHttp.responseText
else
document.getElementById(shows).style.display="none"
var the_node = document.getElementById(shows);
execJS(the_node);
if(document.getElementById(trashid))
{
xmlHttp2.send(null);
}
}
else
{
document.getElementById(shows).innerHTML="<br><br><div class=\"a9bl\" align=\"center\"><font color=black>Loading, please wait...</font><div>"
}
}

and


function GetXmlHttpObject()
{
var req = false;
// branch for native XMLHttpRequest object
if(window.XMLHttpRequest && !(window.ActiveXObject))
{
try {
req = new XMLHttpRequest();
} catch(e) {
req = false;
}
// branch for IE/Windows ActiveX version
}
else if(window.ActiveXObject)
{
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
req = false;
}
}
}
return req
}

This is the code for my button:

<input name="submit_rating_done" value="Submit" onclick='return morerating("ratingform", false, "rating_main_block_editorial"); return false;' type="submit">

I'd be quite grateful for any pointers! Thank you for your time!

knkk

8:20 am on Dec 22, 2008 (gmt 0)

10+ Year Member



Okay, I got it - I was returning true when I should have been returning false. So this was the issue:


function morerating(ratingform, checkflag, loc)
{
//some code...
xmlHttp.onreadystatechange=stateChangedALL;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
[COLOR="Red"]return false[/COLOR]; /*this was "true" earlier*/
}
else
{
return false;
}
}

Still, though, why was the alert making it work?