SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Variable is changing by itself?

    I'm a noob with Javascript, so be kind.

    The variable system2 works until the line:
    Code:
    }).done(function( html ) {
    After this line, it is using the last entry in the array rather then the proper entry. It is as if the 'for' statement has closed, yet it hasn't. Help?

    Code:
    function searchw(){
    
    	var search = document.getElementById("walks").value;
    
    	var systems = "'.$done4.'";
    	var systems = systems.split(";;");
    
    	var systems=cleanArray(systems);
    
    	var length = systems.length;
    
    	var system2;
    
    	for(var i = 0; i < length; i++) {
    
    		system2=systems[i];
    
    		$.ajax({
    		url: "userb.php",
    		data: { user: "'.$user.'", system: system2, page: 1, sort: 1, search: search},
    		cache: false
    		}).done(function( html ) {
    			$("#show"+system2).html(html);
    		});
    
    	}
    
    }

  2. #2
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,287
    Mentioned
    182 Post(s)
    Tagged
    4 Thread(s)
    It is not clear what this function is trying to do.
    There is a reference to
    Code:
    cleanArray()
    , a function that is defined elsewhere.
    It is a poor practice to reassign a variable from the result of an operation
    Code:
    	var systems = systems.split(";;");
    Additionally, this code first defines `systems` without any semicolons and then calls `split` to break it at each semicolon.

    Can you provide a little more context and the rest of the code? It would be much easier to help you.
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  3. #3
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply.

    Here is the cleanArray function and updated code. This function removes the empty values from the array. 'systems' does have semicolons, but before I was only showing the PHP variable - I replaced this with the actual string.

    Code:
    function cleanArray(actual){
      var newArray = new Array();
      for(var i = 0; i<actual.length; i++){
          if (actual[i]){
            newArray.push(actual[i]);
        }
      }
      return newArray;
    }
    
    function searchw(){
    
    	var search = document.getElementById("walks").value;
    
    	var systems1 = ";;1;;;;15;;;;4;;;;23;;;;29;;;;184;;";
    	var systems = systems1.split(";;");
    
    	systems=cleanArray(systems);
    
    	var length = systems.length;
    
    	var system2;
    
    	for(var i = 0; i < length; i++) {
    
    		system2=systems[i];
    
    		$.ajax({
    		url: "userb.php",
    		data: { user: "'.$user.'", system: system2, page: 1, sort: 1, search: search},
    		cache: false
    		}).done(function( html ) {
    			$("#show"+system2).html(html);
    		});
    
    	}
    
    }

  4. #4
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,287
    Mentioned
    182 Post(s)
    Tagged
    4 Thread(s)
    First, it seems you are doing a lot of unnecessary work.
    You are

    1. Defining a string (that contains semicolons as delimiters
    2. Splitting the string
    3. Removing empty elements due to the fact that the string you defined contains a varying number of delimiters
    4. Only to pass the resultant values to a JSON object


    Wouldn't it make sense to just pass list of values (1,15,4,23,29,184) to the ajax call?
    You could do something like this:

    Code:
            var systems = new Array(1,15,4,23,29,184);
    	for(var i = 0; i < systems.length; i++) {
    
    		$.ajax({
    		url: "userb.php",
    		data: { user: "'.$user.'", system: systems[i], page: 1, sort: 1, search: search},
    		cache: false
    		}).done(function( html ) {
    			$("#show"+system2).html(html);
    		});
    
    	}
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  5. #5
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I knew I should have cleaned that up.

    My new code is below. Note the alert(systems[i]) which returns 'undefined'.

    systems[i] is defined earlier, and it works when it is first used, but it does not work where the alert() is called.

    PHP Code:
    function searchw(){

        var 
    search document.getElementById("walks").value;
        var 
    systems = new Array(1,15,4,23,29,184);

        for(var 
    0systems.lengthi++) {

            $.
    ajax({
            
    url"userb.php",
            
    data: { user"user"systemsystems[i], page1sort1searchsearch},
            
    cachefalse
            
    }).done(function( html ) {

    alert(systems[i]);

                $(
    "#show"+systems[i]).html(html);
            });

        }



  6. #6
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Found the problem.

    I need "async: false" or else the "i" was returning "6" each time.

    Thanks for the help!


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
  •