www.webdeveloper.com
Results 1 to 12 of 12

Thread: Cant properly pass the variable

  1. #1
    Join Date
    Jul 2012
    Posts
    41

    Cant properly pass the variable

    not to much to read about this on the internet so maybe someone could help me
    ajax
    Code:
    <script type="text/javascript">
    function showUser()
    {
    
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    
     var id = '1';
    
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("row5").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","getteams.php?" +id,true);
    xmlhttp.send();
    
    }
    </script>
    getteams.php
    PHP Code:
    $sa$_GET['$id']; 
    gets me identified index $id .

  2. #2
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    Your PHP is looking for a URL formatted like this:
    Code:
    yourdomain.com/getteams.php?$id=N
    Your javascript, on the other hand, is request a URL like this:
    Code:
    yourdomain.com/getteams.php?N
    So, I'd suggest two changes. Change your PHP to this:
    PHP Code:
    $sa$_GET['id']; 
    And change the open() method in your JavaScript to this:
    PHP Code:
    xmlhttp.open("GET","getteams.php?id=" +id,true); 
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

  3. #3
    Join Date
    Jul 2012
    Posts
    41
    Thank you for the explanation. it worked. do i get it right if i would want to send multiple variables it should look like
    PHP Code:
    xmlhttp.open("GET","getteams.php?id=" +id +id2 +id3,true); 

  4. #4
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by Stobor View Post
    Thank you for the explanation. it worked. do i get it right if i would want to send multiple variables it should look like
    PHP Code:
    xmlhttp.open("GET","getteams.php?id=" +id +id2 +id3,true); 
    Just so you know, that would translate to:

    Assuming id = 12, id2 = 24, & id3 = 500;
    Code:
    getteams.php?id=1224500
    I don't know for sure but I think that may not be what you want.

  5. #5
    Join Date
    Jul 2012
    Posts
    41
    Understood. One more thing why this gives back only the last one (16) team. In my head it should fill all 16 divs with appropriate teams but in only fills the last div with the last team.
    Code:
     function counter(){
     for (i=1;i<17;i++){
    
     team = "team"
    
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
     id = team+i;
    
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById(id).innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","getteams.php?id=" +i,true);
    xmlhttp.send();
    
    
    }}

  6. #6
    Join Date
    Jan 2009
    Posts
    3,346
    Where is the code that handles filling the divs? I'm guessing you are overwriting the contents of one div each time.

  7. #7
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by criterion9 View Post
    Where is the code that handles filling the divs? I'm guessing you are overwriting the contents of one div each time.
    Just saw that you are overwriting the actual request each time through the loop so only the last one is executed.

  8. #8
    Join Date
    Jul 2012
    Posts
    41
    okey i fixed itbut it works kinda strange
    Code:
    var i = '0';
     function counter(){
    alert (i);
    i++;
    showUser(i);
    }
     
     
     function showUser(i){
    
     team = "team"
    
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
     id = team+i;
    
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById(id).innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","getteams.php?id=" +i,true);
    xmlhttp.send();
    if(i<'17'){counter();}
    }
    if i remove alert(i); part then nothing works and divs remain empty. but if i leave it on after each alert next div fills up. ANy ideas on how to fix it ?
    Last edited by Stobor; 07-30-2012 at 01:05 PM.

  9. #9
    Join Date
    Jan 2009
    Posts
    3,346
    You are still overwriting your handles if you don't have the alert in there to give it time to complete the asynchronous request.

  10. #10
    Join Date
    Jul 2012
    Posts
    41
    Well i have no idea what the syntax should be not to overwrite.

  11. #11
    Join Date
    Jan 2009
    Posts
    3,346
    As an example...you have a global "i" which you are incrementing. You are using an asynchronous action, which means that once the action is fired control returns to the loop to fire the next asynchronous action. Somewhere in there your complete event is fired. That function also uses the global "i", the question is: what will the value be of "i" at that point?

  12. #12
    Join Date
    Mar 2007
    Location
    Canada
    Posts
    99
    Man, why are you trying to reinvent the wheel?
    Check jQuery and do the ajax with it. Will save you future troubleshooting as it works across different browsers.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles