AJAX is common now a days. People try to implement AJAX but fail to return the response from AJAX call successfully because they misunderstood the concept behind AJAX and the right way to do that.
Here I have tried to explain the flow and restructure the code for AJAX to let your application work. First I have explained the problem then the meaning of AJAX and flow as well followed by the solution.