views:

44

answers:

1

Hi friends I need a simple php script like it should start with myname01 to myname-9999999 numbers.

But it should be like this 

01

02

03

04

. . . .

9999999

And on a button click I want to some like this. user is given a javascript prompt to enter desired counter value Like if the user put value 4 in the js propmt then my php counter will be look like this 01

02

03

04

Please help solve this problem. Thanks

+1  A: 

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.

steven_desu
thanks steven am really a newbie and don't have such skills like you. thanks a lot man. I was really surprised by the behavior of BGerrissen. But I think people like you are the spark plugs of this forum and IT help forums.
junjua
@junjua One recommendation if you want people to respect you and answer your questions a bit more, make sure to always accept one of the answers someone gives =) 38% is a very low accept rate. Most people don't answer questions if you have a low accept rate. It usually means you like to show up, ask a question, and leave without giving back to the community.
steven_desu