Can anyone help with some Javascript?

zimu

Diamond Member
Jun 15, 2001
6,209
0
0
was hoping someone here could help.

basically have a page with a form, lets say you can put in a number and that's how many times a for loop will run (in php) when you hit "submit".

all i want is a small javascript generated "number" that obviously starts at 0 when you first load the page (since you havent' hit submit), and then when you hit for e.g. "10" and "submit" it'll show you the counter increasing up to 10, which increments each time the post-processing script runs its loop.

yes, you'd think it'd be too fast, but its a processor intensive script and can take from 2-5 seconds to run a single round.

Thanks guys :)
 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
Originally posted by: zimu
was hoping someone here could help.

basically have a page with a form, lets say you can put in a number and that's how many times a for loop will run (in php) when you hit "submit".

all i want is a small javascript generated "number" that obviously starts at 0 when you first load the page (since you havent' hit submit), and then when you hit for e.g. "10" and "submit" it'll show you the counter increasing up to 10, which increments each time the post-processing script runs its loop.

yes, you'd think it'd be too fast, but its a processor intensive script and can take from 2-5 seconds to run a single round.

Thanks guys :)


I don't think you are understanding how php and javascript interacts. Once the form submits it'lll redirect to another page that can be php generated. It is then that the php will run, it won't run while the previous page is being displayed to the user, i.e. handle an onclick/onsubmit event.
You don't need any javascript at all, what you could do is once the form is submitted, immediately echo the following:

 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
Damn AT forums won't even let me post HTML inside an attach code thingy.... sorry things look screwey up there ^^^.
 

zimu

Diamond Member
Jun 15, 2001
6,209
0
0
ok i'm unfortunately a n00b when it comes to javascript. after i had posted though i did think of the code needing to be on the post processing page...

so i don't REALLY get your attached code. ok, this form that is being submitted, it comes back to the same page, so could you give me maybe a bit more info as to how to impelment it?

ok so in pseudocode (except the javascript bit):


[head>
[script type="text/javascript">
function hideCounterDiv(){
div = document.getElementById('counter');
div.style.visibility = "hidden";
}
[/script>
[/head>
[body onload

if (test to see if form was submitted)
{ for loop here for what needs to be done, with a variable counter? (i think counter- same variable name as what you put in the javascript above?

}



[form starts here] [submit button here]

[/body>[/html>




not too sure where the counter would be displayed, and whether it would increment itself if i use the same variable name for the counter in the for loop?
 

igowerf

Diamond Member
Jun 27, 2000
7,697
1
76
PHP is server side code and the Javascript is client side. This means that the PHP code is executed first on the server to generate the HTML page, and then the Javascript executes on the user's machine when he or she receives the HTML page. While the PHP code is executing, the client has to wait for the page to load.

If you really want this counter, you could do it in a little more complex way. Google around for AJAX. It basically uses Javascript to call and retreive information from PHP pages. This way, you can have the user's browser responsive at all times while PHP is crunching away on the server side.
 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
OK, are you clear that PHP and Javascript variables have absolutely no interaction? Once PHP generates the page javascript cannot call PHP in anyway whatsoever. The only thing javascript can do is to submit a form and set Form values that PHP can then process. So

See below
 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
BTW what this does is: [body <?php echo ($doForLoop ? "onload=hideCounterDiv();") : "" ;?> >
call the hideCounterDiv() function (in JS) when the page finishes loading (i.e. after your PHP script sends the </html> tag)... since your PHP will take a long time running the page will not finish loading, and the browser will most probably show whatever it is recieving (the text in the div block). Once the page is loaded the progress div block is not longer needed so it can be hidden.