SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast Labrocca's Avatar
    Join Date
    Feb 2004
    Location
    Las Vegas
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help Appreciated for ajax and IE problem

    I have a mod for mybb forums called Invite User. Basically it allows for you to invite a member with a referral link from your usercp. It utilizes ajax to add more invite boxes. Works great in Opera and FF. However I just noticed it's NOT working in IE. The first box doesn't appear. Now on the html there is a body onload call for the function AddEmailBox(). However in IE I get a runtime error for the line I have bolded below. Any help is greatly appreciated. I tried to figure it out myself but basically just wasted 2 hours.


    Code:
    var boxcount= "0";
    
    function AddEmailBox()
       {
          old=new Array();
          for(i=0 ; i < boxcount; ++i)
            old[i]=document.getElementById('email'+i).value;
            
          document.getElementById("frm").innerHTML+='<tr><td class="trow2" width="30%"><strong>'+inv_email+'</strong></td><td class="trow2" width="30%"><input type="text" class="textbox" name="emails[]" size="25" maxlength="150" id="email'+boxcount+'"/></td><td class="trow2" id="msg'+boxcount+'" width="40%"></td></tr>';
          
          for(i=0 ; i < boxcount; ++i)
            document.getElementById('email'+i).value=old[i];
            
            ++boxcount;
            
       }
    
    function send(email,result)
    {
        b='email='+email;
        new ajax('xmlhttp.php?action=invite',{method:'post',postBody:b,update:result});
    }
       
    function invite(form)
    {
        for(i=0 ; i< form.elements.length;++i)
            form.elements[i].disabled=true;
            
        emptyAll=true;
        for(i=0 ; i < boxcount; ++i)
        {
            inp=document.getElementById('email'+i);
            Result=document.getElementById('msg'+i);
            send(inp.value,Result);
        }
        
        for(i=0 ; i< form.elements.length; ++i)
            form.elements[i].disabled=false;
    }
    
    function resetAll()
    {
        document.getElementById('frm').innerHTMl="";
        boxcount=0;
        AddEmailBox();
        
    }

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your code works fine as long as long as there are elements with all the id's between "email0" and "email<boxcount>":
    Code:
    <html>
    <head><title></title>
    
    <script type="text/javascript">
    //<![CDATA[
    
    var boxcount= "2";
    
    function AddEmailBox()
    {
      old=new Array();
      for(i=0 ; i < boxcount; ++i)
    	old[i]=document.getElementById('email'+i).value;
    
      alert(old);
    }
    
    window.onload=function()
    {
    	document.getElementById("b0").onclick = AddEmailBox;
    	
    };
    
    //]]>
    </script>
    </head>
    <body>
    
    <form name="f" method="post" action="">
    
    <input type="text" name="" id="email0" value="hello"/>
    <input type="text" name="" id="email1" value="world"/>
    
    <input type="button" id="b0" value="start test"/>
    
    </form>
    
    </body>
    </html>

  3. #3
    SitePoint Enthusiast Labrocca's Avatar
    Join Date
    Feb 2004
    Location
    Las Vegas
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well...thanks for the reply but not sure it helps me. The code DOESN'T work for IE as it stands. Would you know how I can alter this javascript to show all the element ID's? I apologize for being so terrible with javascript. I mostly do php programming.

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2006
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What he's trying to say is that IE is coughing at that line because until you have any input boxes added, document.getElementById fails. My suggestion is, modify that function to something like:

    Code:
    function AddEmailBox()
       {
          old=new Array();
          for(i=0 ; i < boxcount; ++i) {
            try {
                old[i]=document.getElementById('email'+i).value;
            } 
            catch(e) {
                // do nothing because no boxes exist.
            }
          }
          document.getElementById("frm").innerHTML+='<tr><td class="trow2" width="30&#37;"><strong>'+inv_email+'</strong></td><td class="trow2" width="30%"><input type="text" class="textbox" name="emails[]" size="25" maxlength="150" id="email'+boxcount+'"/></td><td class="trow2" id="msg'+boxcount+'" width="40%"></td></tr>';
          
          for(i=0 ; i < boxcount; ++i) {
            document.getElementById('email'+i).value=old[i];
          }
            ++boxcount;
            
       }
    Note that I've automatically added braces to your for loop to make it less confusing to read and debug.

    Dave

  5. #5
    SitePoint Enthusiast Labrocca's Avatar
    Join Date
    Feb 2004
    Location
    Las Vegas
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I really appreciate your help but that doesn't seem to fix the problem. I get the same error. Is it possible this is the error line?

    for(i=0 ; i < boxcount; ++i) {


Bookmarks

Posting Permissions

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