Restructure with PHP code and JavaScript validation