0
<html>
<head>
<script language="javascript">
    function validatefrm(){

        for (var i= 0 ; i< 3 ; i++){

        window.alert(i+window.document.contact[i].value); 
        }

        return false; //to prevent form from submitting for debugging
    }
</script>
</head>
<body>
<form name="editform" onsubmit="return validatefrm();" method="POST">
<?php

for($i=0; $i<3; $i++){
    print "<input type='textbox' id='contact".$i."' value='".$i."'>";
}
?>
<input type="submit" value="Submit Values">
</form>
</body>
</html>

Hi, I'm new to php and javascript. I'm trying to call the form with the value of 0,1,2 with javascript but it wont work. Unless i delete the for loop in javascript function and hard code it as Window.alert (window.document.contact0.value) and so on....Anyone can help? Must appreciate.

3
  • From the title, it made me feel its a duplicate of a question I answered. But it seems its not, can be a little more specific what you want, and where its failing? Commented Nov 24, 2013 at 18:46
  • Hi, ya i got read through your post. But i think mine is different, i'm trying to get 3 input from the form like 0,1,2 as the value...then once submit button is click...the window alert should also print out value that i have store in the form such as 0,1,2 Commented Nov 24, 2013 at 18:50
  • Yes, I understood you question after I posted that comment. And I have answered below. HTH Commented Nov 24, 2013 at 18:52

4 Answers 4

1

If I get your question correctly, you need to access the elements by ID. And the correct way is to use document.getElementById(...). In your case:

for (var i= 0 ; i< 3 ; i++){
    window.alert(document.getElementById('contact' +i).value); 
}
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry for any inconvenient.
No problem, glad I was able to help :-)
1

You are setting an ID for each element with php contact0, contact1 etc.

So in javascript look for that ID

function validatefrm(){

    for (var i= 0 ; i< 3 ; i++){
        window.alert(i+ ' '+ document.getElementById('contact'+i).value); 
    }
    return false; //to prevent form from submitting for debugging
}

Comments

0

Your best choice would most likely be to use json_encode. This method outputs the array as a JSON array which can be assigned to a JavaScript variable in <script> tags. Associative arrays on the PHP side would get converted into JSON objects.

json_encode in the PHP documentation

<?php
$myArr = array(0, 1, 2);
?>

<!-- somewhere in the HTML document -->
<script>
   var myArr = <?php echo json_encode($myArr); ?>;
   // do something with myArr
   console.log(myArr);
</script>

Comments

0

The php is executed on the server side, before it makes it to the browser. The javascript is executed on the client side, in the browser. You cannot pass php values to javascript like this.

You can make the php output javascript (in a script tag), and use that in your javascript code though.

<script>
  var foo = [];
  <?php
    for($i=0; $i<3; $i++){
      print "foo[".$i."] = ".$i;
    }
  ?>
console.log(foo[0] + ", " + foo[1] + ", " + foo[2]);
<script>

1 Comment

I think his question is completely different. He wants to know why his for loop in javascript isn't working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.