0
<html>
    <head>
    <script>
        function showUser()
        {
            for (var i = 0; i < 10; i++)
            {
                var internal = document.getElementById("inte[i]").value;   /* but its not working */
                var external = document.getElementById("exte[i]").value;
            }
        }
    </script>
    </head>
    <body>
    <?php 
        for (i = 0; i < 10; i++)
        {
            echo "<td><input type='text' name='internal' id = 'inte[$i]' width='30'/> </td>";
            echo "<td><input type='text' name='external' id = 'exte[$i]' width='30'/> </td>";
        }
        echo "<td><input type='submit' name='submit1' value='Save Marks' width='30'  onclick = 'showUser()' /></td>";
    ?>
    </body>
<html>

i am using the above code but i cant get 10 values of different textboxes how to fetch these value through java script and i want to save it to database kindly help me

i am using the above code but i cant get 10 values of different textboxes how to fetch these value through java script and i want to save it to database kindly help me

4
  • 1
    There is an unclosed comment in the for loop. Commented Oct 3, 2013 at 14:15
  • The [i] in the document.getElementById("intel[i]") doesn't get reffered to the i. Its is evaluated as find the element with the id "intel[i]" To fix this you would remove the quotes. Commented Oct 3, 2013 at 14:16
  • you php for loop is incorrect (i=0;i<10;i++) should be ($i=0;$i<10;$i++) Commented Oct 3, 2013 at 14:17
  • @Bala Krishnan view my posted answer Commented Oct 3, 2013 at 14:25

3 Answers 3

1

You are not placed i value in getElementById

Replace your

document.getElementById("inte[i]").value;

as

document.getElementById("inte["+i+"]").value;

Make your for loop as

for (var i=0; i<10; i++)
{
  var internal = document.getElementById("inte["+i+"]").value;   /* but its not working  */              
  var external = document.getElementById("exte["+i+"]").value;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I think he is looking it up in an array defined somewhere.
1

This isn't doing what you think:

getElementById("inte[i]")

That's just a string, the i value doesn't get interpreted into an integer. This would:

getElementById("inte[" + i + "]")

Comments

0

When your Javascript for loop runs you are are overwriting your var internal and external each time so after the for loop runs you will only have the last value that was assigned to the variables. You should use an array and push the values of the text boxes into it.

var internal = new Array();
var external = new Array();

for (var i=0; i<10; i++)
{
  internal.push(document.getElementById("inte["+i+"]").value);   /* but its not working  */              
  external.push(document.getElementById("exte["+i+"]").value);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.