bobleny Posted February 20, 2008 Share Posted February 20, 2008 Hi! I have a form with inputs who's names are like this: boxy_1, boxy_2, boxy_3, boxy_4, etc... I know that there are 1000 inputs with this name. What I need to do is, print the inputs values. To do this, I use a for loop; for(var i = 1; i <= 1000; i++) { document.getElementById("message").innerHTML += form.boxy_i.value; } So, you see the problem yet? The variable "i" is not being echoed into, "form.boxy_i.value". So javascript is looking for a form input named "boxy_i", when it should be looking for an input named "boxy_1", "boxy_2", "boxy_3", "boxy_4", etc... My question is, how do I fix this? Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted February 20, 2008 Share Posted February 20, 2008 try this: for(var i = 1; i <= 1000; i++) { document.getElementById("message").innerHTML += document.form.boxy_+i+.value; } Quote Link to comment Share on other sites More sharing options...
bobleny Posted February 20, 2008 Author Share Posted February 20, 2008 Yeah, I tried that. It doesn't work... I believe it is a syntax error.... Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted February 20, 2008 Share Posted February 20, 2008 ok; hold on, let me put it to the test; didn't test it before - be right back in a few Quote Link to comment Share on other sites More sharing options...
bobleny Posted February 20, 2008 Author Share Posted February 20, 2008 Playing around, I was able to get close.... var fro = "form.boxy_"; var end = ".value"; var che = fro+i+end; che = form.boxy_1.value, form.boxy_2.value, form.boxy_3.value, etc... however, "form.boxy_1.value" is different from form.boxy_1.value..... Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted February 20, 2008 Share Posted February 20, 2008 How about we try it like this: <script language="javascript"> function doIt() { for (i=1; i<=2; i++) { document.getElementById("message").innerHTML += document.getElementById("boxy"+i).value + " "; } } window.onload=function() { doIt(); } </script> <form> <input type="text" id="boxy1" value="Hello"> <input type="text" id="boxy2" value="World"> </form> <div id="message"></div> Can you live with it this way? Of course you will want to change the script around to the way you want it to be; but the above is just for demo purposes. Quote Link to comment Share on other sites More sharing options...
bobleny Posted February 20, 2008 Author Share Posted February 20, 2008 That doesn't seem to work for me... The form is actually being echoed in through javascript. Which is apparently causing unique issues... Whenever I use that method, it simply refreshes the page... Any other ideas? Quote Link to comment Share on other sites More sharing options...
bobleny Posted February 20, 2008 Author Share Posted February 20, 2008 It seems to me that the only way to do this is to figure out how to get form.boxy_i.value to work... Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted February 20, 2008 Share Posted February 20, 2008 It works fine for me; it is getting all the values from your input fields, that have an id of "boxy"; plus the corresponding number, that goes along with the id. The form is not even in play with this script; it is pulling values ("echo"ing is you call it; but that is a PHP term) from input fields that have the "boxy" (plus number) id and displaying the values in your "message" div. Doesn't refresh the page for me; that issue must be coming from somewhere else in you script/page. Did you add the ids to each of the input fields; like the way I showed you in the demo? Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted February 20, 2008 Share Posted February 20, 2008 Here is another example of how it would work, but let me warn you now; if your trying to get the values of this many input fields at one time; your going to freeze up people's browsers (due to all the memory this script would take up; once it initiated). <script language="javascript"> function doIt() { for (i=1; i<=1000; i++) { document.getElementById("message").innerHTML += document.getElementById("boxy"+i).value + " "; } } </script> <form> <script language="javascript"> for (i=1; i<=1000; i++) { document.write('<input type="text" id="boxy'+i+'" value="Hello">'); } </script> <br><br> <input type="button" onclick="doIt()" value="Return Values"> </form> <div id="message"></div> Quote Link to comment Share on other sites More sharing options...
bobleny Posted February 20, 2008 Author Share Posted February 20, 2008 Well, this is how the script works. A user enters a directory and clicks submit. This then calls a PHP script that creates a form. It then uses ajax to echo the form on to the current page. This form is a list of check boxes. As far as I know, a check box's can't carry a string value, so I use a hidden field to hold the values. The user then checks the boxes next to the file that they want to delete. I got all of that.... Next, I need to determine which boxes are checked. Then I need to send the value of the coinciding hidden field to another php script that will delete the appropriate files. Here is the script: <html> <head> <title>Data Deleter!</title> <script type="text/javascript"> <!-- var GetServer function Connect() { try { // Firefox, Opera 8.0+, Safari GetServer = new XMLHttpRequest(); } catch (e) { // Internet Explorer try { GetServer = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { GetServer = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { return false; } } } } function Make(info) { Connect(); GetServer.onreadystatechange=function() { if(GetServer.readyState == 4) { document.getElementById("sep").innerHTML = "<hr align='left' width='37%' />"; document.getElementById("message").innerHTML = GetServer.responseText; } } GetServer.open("GET","file_seek.php?loc="+info.loc.value, true); GetServer.send(null); document.formy.loc.focus(); return false; } function checkAll(value) { var inputs = form.getElementsByTagName('input'); for (var i = 0; i < inputs.length; i++) { if (inputs[i].type == "checkbox") { inputs[i].checked = value; } } } // info is there just in case I need it... function Delete(info) { for(var i = 1; i <= form.found_files.value; i++) { // At the moment, I am simply trying to get the correct data // to be displayed for visual inspection. document.getElementById("message").innerHTML += form.boxy_i_value.value +"<br />"; } return false; } function focus() { document.formy.loc.focus(); } //--> </script> </head> <body onload='focus()'> <form name='formy' onsubmit='return Make(this)' method='post'> Directory: <input type='text' name='loc' size='50' /> <hr> <input type='Submit' value='Send'> </form> <div id='sep'></div> <div id='message'></div> <div id='sep2'></div> </body> </html> By echoed, I mean PHP is literally echoing this form line by line for ajax to receive.... And here is the echoed form: <form name='form' onsubmit='return Delete(this)' method='post'> <input type='checkbox' id='checkbox' name='boxy_1' /> <input type='hidden' id='hidden' name='boxy_1_value' value='someLink' /> <input type='checkbox' id='checkbox' name='boxy_2' /> <input type='hidden' id='hidden' name='boxy_2_value' value='somLink' /> <input type='hidden' name='found_files' value='1000' /> <input type='checkbox' name='all' onclick='checkAll(this.checked)' />Select/Deselect All (1000) Items <input type='Submit' value='Delete Selected Items'> </from> ------------------------- Now, I don't care how I accomplish this in terms of programming, I just would like to get this done... P.S. 1000 files is crazy, there never actually be that many, but you never know.... Quote Link to comment Share on other sites More sharing options...
bobleny Posted February 20, 2008 Author Share Posted February 20, 2008 Oh my god, I got it to work! I was bored waiting for a reply, so I was reading Mike Kruse's javascript dos and doents. and he started to talk about dot notation and square bracket notation. As it turns out... This: form.boxy_1 is the same as this: form["boxy_1"] Which allows me to do this: form["boxy_"+i].checked and this: form["boxy_"+i+"value"].value I knew there was a way to do it! All that time.... :'( All well, at least I now know.............. Thanks for your help PHP question man! --------- P.S. I will click on the "Topic Solved" button as soon as it makes its self avalable... Stupid button... Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted February 20, 2008 Share Posted February 20, 2008 Well you didn't say anything about checkboxes before; you just said you had a form with inputs - got to explain a little more specifically in the future; like you did in your above post. But I guess the main thing is; that you got it working. Quote Link to comment Share on other sites More sharing options...
bobleny Posted February 21, 2008 Author Share Posted February 21, 2008 It shouldn't matter if it is a checkbox or not... I needed to get the info from both the checkbox and the hidden values. It is very simple to convert the one to the other. The issue was echoing information into a variable from a form. That is really all the information that should be necessary for this situation: form["boxy_"+i].checked Because of that, I do limit the amount of information I give. This, "form["boxy_"+i].checked", is the answer I was looking for. Had I given you the entire situation as I did in reply #10, you may have came up with a solution for my problem. However, that solution would most likely have been something other than, "form["boxy_"+i].checked". So I have to ask you, had I given you all the information that I gave you in Reply #10 at the start, would you have thought of this line?: form["boxy_"+i].checked The reason I eventually did explain the entire situation was because, I figured some solution is better that no solutions.... Quote Link to comment Share on other sites More sharing options...
phpQ2 Posted February 21, 2008 Share Posted February 21, 2008 phpQuestioner wrote you a good script and it does work; so don\'t knock people when they ask you for some info. Otherwise you will not get help from people or the help they give you will not turn out the way you wanted it to be. I guess the later would apply to you, in this case. Quote Link to comment Share on other sites More sharing options...
bobleny Posted February 21, 2008 Author Share Posted February 21, 2008 You mean you made an account just to scold me....? That's not cool... :'( I meant no disrespect to any one. I was simply explaining why I don't usually come out with as much detail as possible. And actually, I have found that too much detail can complicate things... phpQuestioner's script did work, I tried it. However, do to the unique situation of my script, his script simply wouldn't work. I actually took his idea and tried it several different ways to suit my needs. It didn't work. I have nothing against phpQuestioner and think he is very good with javascript and I really do appreciate his help. I came forth with the whole script because I began to feel that it was necessary in order to get a different response... I have in the past ask for help with too much information at the start. This can lead to miss understandings, confusions, and many suggestions that may work, but often not quite the way I want them to. So, I give the information that I feel is necessary to get the response I'm looking for. It is far from phpQuestioner's fault that his suggestions didn't work. Again, I am sorry if you felt this was an attack on phpQuestioner. It was not. I also hope that I will be able to count on his help in the future! ----------------- P.S. In case you where wondering, this is how I ended up passing the data on to the PHP file to be deleted! function Delete() { var trail = "?foundFiles="+form.found_files.value; for(var i = 1; i <= form.found_files.value; i++) { if(form["boxy_"+i].checked == true) { trail += "&boxy_"+ i +"_value="+ form["boxy_"+ i +"_value"].value; } } Connect(); GetServer.onreadystatechange=function() { if(GetServer.readyState == 4) { document.getElementById("message").innerHTML = GetServer.responseText; } } GetServer.open("GET","file_delete.php"+trail, true); GetServer.send(null); timer(); return false; } Thanks again for all of your help! "Now where is that darned Topic Solved button....!" Quote Link to comment 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.