View Full Version : Help - Odd count issue

Jun 28th, 2012, 10:37 PM
Hi All,

Im new here, im not very good at JS and mainly do PHP however i have needed to make a quick script however im not very good at it.

I am sending a string to permission_groups then spliting this into 4 arrays in JS. when the alert for number comes up it counts correctly 0,1,2,3. I am putting this as the array value to get array text 0,1,2 and 3 however only when it runs the first time and uses 0 it works fine but when it runs the following times i get only the first letter of the array's in the fields "group[number]". I get the following "<option value='o'>o</option>" then the next alert will be "<option value='t'>t</option>" when it should be "<option value='Manager'>Manager</option>". If i do an alert for "group[3]" it works fine and gives me the full text. :S

<script type="text/javascript">
function changefield(unique_id,userid,active,f_name,l_name,contact_n,contact_e,permission_group_name, permission_groups){

var group=permission_groups.split("_");


count = group.length;

number = 0;

while (count>0){
group = "<option value='"+group[number]+"'>"+group[number]+"</option>";

document.getElementById(unique_id+"user_id").innerHTML = "<input id=\"user_id\" type=\"text\" name='"+unique_id+"_user_id"+"' value='"+userid+"' />";
document.getElementById(unique_id+"active").innerHTML = "<input id=\"active\" type=\"text\" name='"+unique_id+"_active"+"' value='"+active+"' />";
document.getElementById(unique_id+"f_name").innerHTML = "<input id=\"f_name\" type=\"text\" name='"+unique_id+"_f_name"+"' value='"+f_name+"' />";
document.getElementById(unique_id+"l_name").innerHTML = "<input id=\"l_name\" type=\"text\" name='"+unique_id+"_l_name"+"' value='"+l_name+"' />";
document.getElementById(unique_id+"contact_n").innerHTML = "<input id=\"contact_n\" type=\"text\" name='"+unique_id+"_contact_n"+"' value='"+contact_n+"' />";
document.getElementById(unique_id+"contact_e").innerHTML = "<input id=\"contact_e\" type=\"text\" name='"+unique_id+"_contact_e"+"' value='"+contact_e+"' />";
document.getElementById(unique_id+"permission_group_name").innerHTML = "<select name='"+unique_id+"_permission_group_name"+"'>"+group+"<option value='blblb2'>permission set 2</option></select>";
} // End function changefield

Old Pedant
Jun 28th, 2012, 11:25 PM
BECAUSE YOUR OWN CODE is *changing* the array group into a STRING on the first time through the loop!!!

group = "<option value='"+group[number]+"'>"+group[number]+"</option>";

After the first such assignment, group is now a string, and so then when you use group[number] you WILL get ONE CHARACTER.

Your code is horribly horribly complex, anyway.

Why would you send a delimited string from PHP instead of just sending an array, to start with?

And why is your loop so complex?

Why not simply:

function changefield(unique_id,userid,active,f_name,l_name,contact_n,contact_e,permission_group_name, permission_groups)
var groupArray =permission_groups.split("_");
var group = "";
for ( var g = 0; g < groupArray.length; ++g )
group += '<option value="' + groupArray[number] + '">' + groupArray[number] + '</option>';

Jun 29th, 2012, 08:15 AM

thanks that worked a treat :), i can see where i went wrong now. Didnt think about the name but all makes sence now.

Thanks for the post :D