I'm not real sure what you are trying to accomplish with that code, but I would expect you never get any value other than "1"? Once the page is displayed on the browser, the PHP is done and all variables and other script data are wiped from the server's memory, so any subsequent call of the page will start with $i being undefined and then incremented to 1 (probably with a notice-level message being generated if error_reporting(E_ALL) is enabled for the unitialized variable).
You either need to have your button call the script again, but sending the current value as a GET or POST value that can then be used to initialize $i in your PHP script, or else use an AJAX approach to do essentially the same thing (with a bit more code but a bit less internet traffic).