Variable is changing by itself?

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

The variable system2 works until the line:

}).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?

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);
		});

	}

}

It is not clear what this function is trying to do.
There is a reference to

cleanArray()

, a function that is defined elsewhere.
It is a poor practice to reassign a variable from the result of an operation


	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.

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.

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);
		});

	}

}


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:


        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);
		});

	}

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.

function searchw(){

	var search = document.getElementById("walks").value;
	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 ) {

alert(systems[i]);

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

	}

}

Found the problem.

I need “async: false” or else the “i” was returning “6” each time. :wink:

Thanks for the help!