you can place your script anywhere you want. just make sure the webserver you use will execute them.
The reason is to keep script separately from html just to be more organized. Sometimes security. In reality usually scripts can easily access the directory with html files as they are run by the webserver under the same user.
I keep scripts in the same directory where I keep html files. Blame me other monks, if you think it's not correct.