You don't need PHP to do this. What you're doing is taking the input from the user and performing an iterative mathematic operation. Javascript is capable of both iteration and math. The only reason you should need PHP is if you need a resource from the server.
Try something like the following:
<script type="text/javascript">
function submitIt(){
var countTo = document.getElementById("countForm").elements["countTo"].value;
var countHere = document.getElementById("countHere");
var countHere.innerHTML = '00';
for(var i=0; i<=countTo && i<10; i++){
countHere.innerHTML += '0'+i+'<br />';
}
for(var i=0; i<=countTo; i++){
countHere.innerHTML += i+'<br />';
}
}
</script>
<form id="countForm" action="#" method="#">
Count to: <input type="text" name="countTo" /><br />
<input type="button" onclick="submitIt()" /><br />
</form>
<div id="countHere">
The numbers will show up here
</div>
It's not the most efficient solution by any means, but it should demonstrate the basic premise. Since you wanted to start with 00, 01, 02 (instead of 0, 1, 2...) I created a second loop for appending a 0 if the number was less than 10. There are better solutions that stay in one line, but this one should be good enough to get you started.
Essentially you have to make sure the <script>
is before the <form>
so that the browser already knows what the function submitIt()
is before you click the button. Clicking the button then calls this function, which locates the form element with document.getElementById()
and grabs it's "countTo" element's value. After this we used document.getElementById()
again to find where we wanted to output and we just looped over the values.
Check out this resource on updating text with javascript and The official resource on javascript for loops.