unemployment Posted May 31, 2011 Share Posted May 31, 2011 Any idea why my elements are not being created? Firebug doesn't return any errors and the ajax data is being submitted. if (goal_title !== 'Goal Subject...' && goal_title.length > 0) { var goal_start_date = goal_start_year + '-' + goal_start_month + '-' + goal_start_day; var goal_end_date = goal_end_year + '-' + goal_end_month + '-' + goal_end_day; ajax.post('/assets/ajax/goal_create.php', 'title=' + goal_title + '&status=' + goal_status + '&start_date=' + goal_start_date + '&end_date=' + goal_end_date + '&info=' + goal_info, function(resp) { //Remove all children of previous goals list and recreate the new one from var resp (holds all the data required) // Feel free to change the returned_goals variable name var goals = eval('(' + resp + ')'); var goal_list = document.getElementById('goal_list'); removeChildren(goal_list); alert(goal_list); console.log(goal_list); var goal_item =[]; var goal_holder =[]; var goal_icon =[]; var goal_icon_status =[]; var goal_icon_img =[]; var goal_identifier =[]; var goal_list_items =[]; var goal_ul =[]; var goal_li_uncomplete =[]; var goal_a_uncomplete =[]; var goal_li_onhold =[]; var goal_a_onhold =[]; var goal_li_cancelled =[]; var goal_a_cancelled =[]; var goal_li_completed =[]; var goal_a_completed =[]; var goal_status =[]; var goal_name =[]; var goal_add_info =[]; var goal_end_date =[]; for (i = 0; i < goals[i].length; i++) { alert(goals[i].length); console.log(goals[i].length); goal_item[i] = document.createElement('div'); goal_holder[i] = document.createElement('div'); goal_icon[i] = document.createElement('a'); goal_icon_status[i] = document.createElement('div'); goal_icon_img[i] = document.createElement('img'); goal_identifier = document.createElement('div'); goal_list_items = document.createElement('div'); goal_ul = document.createElement('ul'); goal_li_uncomplete = document.createElement('li'); goal_a_uncomplete = document.createElement('a'); goal_li_onhold = document.createElement('li'); goal_a_onhold = document.createElement('a'); goal_li_cancelled = document.createElement('li'); goal_a_cancelled = document.createElement('a'); goal_li_completed = document.createElement('li'); goal_a_completed = document.createElement('a'); goal_status[i] = document.createElement('input'); goal_name[i] = document.createElement('div'); goal_add_info[i] = document.createElement('div'); goal_end_date[i] = document.createElement('div'); goal_item[i].className = 'goal_item_passed'; goal_holder[i].className = 'goal_icon_holder mrs'; goal_icon[i].href = 'javascript:void(0)'; goal_icon[i].className = 'goal_icon_button'; goal_icon_status[i].className = 'goal_icon uncompleted'; goal_icon_img[i].src = '/assets/img/icons/' + goals[1][i].goal_status; goal_li_completed[i].appendChild(goal_a_completed[i]); goal_li_cancelled[i].appendChild(goal_a_cancelled[i]); goal_li_onhold[i].appendChild(goal_a_onhold[i]); goal_li_uncomplete[i].appendChild(goal_a_uncomplete[i]); goal_ul[i].appendChild(goal_li_completed[i]); goal_ul[i].appendChild(goal_li_cancelled[i]); goal_ul[i].appendChild(goal_li_onhold[i]); goal_ul[i].appendChild(goal_li_uncomplete[i]); goal_list_items[i].appendChild(goal_ul[i]); goal_identifier[i].appendChild(goal_list_items[i]); goal_icon[i].appendChild(goal_identifier[i]); goal_icon_status[i].appendChild(goal_icon_img[i]); goal_icon[i].appendChild(goal_icon_status[i]); goal_holder[i].appendChild(goal_icon[i]); goal_item[i].appendChild(goal_end_date[i]); goal_item[i].appendChild(goal_add_info[i]); goal_item[i].appendChild(goal_name[i]); goal_item[i].appendChild(goal_status[i]); goal_item[i].appendChild(goal_holder[i]); goal_list.appendChild(goal_item[i]); } }); } Quote Link to comment https://forums.phpfreaks.com/topic/237947-elements-are-not-being-created/ Share on other sites More sharing options...
seanlim Posted May 31, 2011 Share Posted May 31, 2011 Are you sure your for loop is correct? for (i = 0; i < goals.length; i++) Is the second statement supposed to be i<goals.length? I have no idea what is in "goals" since I don't have your PHP script's output... Does the alert/console log in the for loop fire? What's the output? Quote Link to comment https://forums.phpfreaks.com/topic/237947-elements-are-not-being-created/#findComment-1222745 Share on other sites More sharing options...
seanlim Posted May 31, 2011 Share Posted May 31, 2011 Sorry, i meant: for (i = 0; i < goals[i].length; i++) the square brackets came out as italics Quote Link to comment https://forums.phpfreaks.com/topic/237947-elements-are-not-being-created/#findComment-1222756 Share on other sites More sharing options...
unemployment Posted May 31, 2011 Author Share Posted May 31, 2011 Sorry, i meant: for (i = 0; i < goals[i].length; i++) the square brackets came out as italics No I don't think my for loop is fireing. I couldn't get an alert to work in it. Quote Link to comment https://forums.phpfreaks.com/topic/237947-elements-are-not-being-created/#findComment-1222802 Share on other sites More sharing options...
KevinM1 Posted May 31, 2011 Share Posted May 31, 2011 Are you sure goals exists? Why are you using eval to obtain it? Quote Link to comment https://forums.phpfreaks.com/topic/237947-elements-are-not-being-created/#findComment-1222816 Share on other sites More sharing options...
unemployment Posted May 31, 2011 Author Share Posted May 31, 2011 Are you sure goals exists? Why are you using eval to obtain it? Yes, goals exists. If I alert goals objects are alerted. I'm only using eval because it's the only way I know how to do it. If you have a better method let me know. Quote Link to comment https://forums.phpfreaks.com/topic/237947-elements-are-not-being-created/#findComment-1222844 Share on other sites More sharing options...
KevinM1 Posted May 31, 2011 Share Posted May 31, 2011 Well, what does resp contain? You should really be using JSON as your response format. Its native JavaScript, and it allows you to treat your response as an object (which it is - that's what the O stands for). Also, take a look at your loop: for(var i = 0; i < goals[i].length; i++) Are you sure you want to check each goal element's length? Or do you really want: for(var i = 0; i < goals.length; i++) ? 99.9999....% of the time, the second version is correct. Quote Link to comment https://forums.phpfreaks.com/topic/237947-elements-are-not-being-created/#findComment-1222851 Share on other sites More sharing options...
unemployment Posted May 31, 2011 Author Share Posted May 31, 2011 Well, what does resp contain? You should really be using JSON as your response format. Its native JavaScript, and it allows you to treat your response as an object (which it is - that's what the O stands for). Also, take a look at your loop: for(var i = 0; i < goals[i].length; i++) Are you sure you want to check each goal element's length? Or do you really want: for(var i = 0; i < goals.length; i++) ? 99.9999....% of the time, the second version is correct. I'll have to look into JSON and see how it works. Have any documentation you could link me to that would give me a quick heads up / tutorial? Thank you for fixing my problem as the second version was what I was looking for. Looks like I got confused as I usually do with JS. Quote Link to comment https://forums.phpfreaks.com/topic/237947-elements-are-not-being-created/#findComment-1222852 Share on other sites More sharing options...
KevinM1 Posted May 31, 2011 Share Posted May 31, 2011 http://www.json.org/js.html And, to encode your PHP response as JSON: json_encode Quote Link to comment https://forums.phpfreaks.com/topic/237947-elements-are-not-being-created/#findComment-1222860 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.