0

I am trying to pass a value from javascript to a field on a form. However, it doesn't seem to work. The html code is:

<html> <head> <title>Insert title here</title> </head> <body onload="splitter()"> <p>Are you sure you want to delete?</p> <form name="myform" action="http://localhost:8080/EfsiDatabase/timer" method="post"> <input type="hidden" name="action" value="delete"> <input type="hidden" name="index" id="index"> <input type="submit" name="submit" value="Delete"> </form> </body> </html> 

On the javascript file, the issue I am having is the last line where I am trying to assign the value to the index input field. No value returns when I do this.

<script type="text/javascript"> function splitter() { var str=window.location.search; var replaced=str.replace("?entry=",""); var n=replaced.split("&entry="); var i=0; var form = document.forms['myform']; while(n) { var x=n[i].split("%7C%7C"); var e1 = document.createElement("input"); e1.type = "hidden"; e1.name = "staff"+i; e1.value = x[0]; var e2 = document.createElement("input"); e2.type = "hidden"; e2.name = "date"+i; e2.value = x[1]; var e3 = document.createElement("input"); e3.type = "hidden"; e3.name = "project"+i; e3.value = x[2]; var e4 = document.createElement("input"); e4.type = "hidden"; e4.name = "task"+i; e4.value = x[3]; var e5 = document.createElement("input"); e5.type = "hidden"; e5.name = "notes"+i; e5.value = x[4]; var e6 = document.createElement("input"); e6.type = "hidden"; e6.name = "hours"+i; e6.value = x[5]; form.appendChild(e1); form.appendChild(e2); form.appendChild(e3); form.appendChild(e4); form.appendChild(e5); form.appendChild(e6); i++; } document.getElementById("index").value=i+1; } </script> 

How can I get a value with this method? Thanks for the help.

6
  • 3
    What do you mean when you say "no value returns"? The element with id="index" is a hidden field. Did you check for script errors? Commented Jun 7, 2013 at 18:27
  • 2
    You have while(n) but you never change the value of n inside the loop. If you get it to work otherwise, that'll be an infinite loop. Commented Jun 7, 2013 at 18:32
  • He means he isn't receiving its value on the back end. Commented Jun 7, 2013 at 18:34
  • 1
    And if you replace "?entry=" by "" you can't split afterwards by "&entry", as long as you don't have two "entry" on the query string. Commented Jun 7, 2013 at 18:46
  • 1
    But you don't change n. This is not a foreach. As far as I can see your while should loop infinite or not even one time, depending on what the split did. Commented Jun 7, 2013 at 18:52

1 Answer 1

2

Instead of

while(n) { var x=n[i].split("%7C%7C"); // ... } 

Please use below code:

while(n[i]) { var x=n[i].split("%7C%7C"); // ... } 
Sign up to request clarification or add additional context in comments.

2 Comments

@Linus Thanks for the Beautify
@user2464576, no problem! I love to help, especially when someone else posts the answer. ;-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.