I didn't do anything that you couldn't have done which is to go through methodically, organize the indents and check that all of the curly braces are matched (Netbeans should help you to do that part). I had to add three more closing curly braces after line 272 to get them to match up. There was another problem with the form at line 277 but it's gone away and I don't know what I did that fixed it. My version of your code now shows no errors.
Maybe you are used to this style of coding but if you are having trouble debugging it yourself then maybe you are making it more complex than it needs to be. PHP let's you do things like you have done with the form action at line 277 (as an example) but personally, I'd make it simpler and not try to embed logic like this in order to derive a variable. It's up to you but personally, I'd rather keep it simple and straightforward because it will probably save time when there is a need to debug it. Just my opinion.
http://innovationsdesign.net/wb_2.7/pages/tech-resources/php-help.php#question_17