I want to create a javascript code for validating dynamic controls i have created from a database using php and html. in the database i have a table with questions and answers, the questions and the options (answers) are created dynamically. But i want to validate these controls with javascript, let's say i have 100 questions, i don't want to create 100 different validations for each questions, even worse if more questions are added to the database they will be added to the page, but i wont't have a way to validate the new answers. How could i create the javascript dinamically as well as the questions and answers? I hope all that makes a little sense.
1 Answer
Try implementing following steps.
- You can create a javascript function which will receive question and answer as parameters.
- Create hidden input type and when you want to validate (for e.g. on submit event) assign values to hidden elements and call the function.
- In javascript function use AJAX code and use you server side script(e.g.PHP,ASP,CGI) to actually validate your question and answer.
- Server side script will return the response and then you can handle it on your html page.