#1
  1. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,969
    Rep Power
    374

    Logic help for displaying questions/answers


    I am going to have a dynamic page (PHP) which will serve users with questions & choices /and or free text boxes.

    What i want to happen is after the user selects a choice / write the answer, the question/answer set "disappears" replaced with % of answers so 76% of other users chose choice 1, etc.

    The thing I need help with is how to actually "do" it. The easier way would be dynamically inserting Jquery before each question

    <script>
    user checks question_id , save the answer in the database (ajax call) disappear the div question_id
    make the other div appear
    </script>

    but i would rather if there was ONE script at the top of the page which would work for all? so instead of using IDs use classes? but not sure on how to go about it.

    another q is about the ajax call, sometime the answer will be a choice, or multiple choices or sometimes free text so should the php script in the backend work out what the answer is? or should i help by sending in an identifier "choice, choices or freetext" or some other way?
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,110
    Rep Power
    119
    Originally Posted by paulh1983
    but i would rather if there was ONE script at the top of the page which would work for all? so instead of using IDs use classes? but not sure on how to go about it.
    Use the jQuery .ready() API; that will DOM ready the entire page. Then, use the jQuery .each() API; to iterate through each element's class.

    Originally Posted by paulh1983
    another q is about the ajax call, sometime the answer will be a choice, or multiple choices or sometimes free text so should the php script in the backend work out what the answer is? or should i help by sending in an identifier "choice, choices or freetext" or some other way?
    You could use conditional validation in the .each() API; to identify each element by it's type and then go from there.

IMN logo majestic logo threadwatch logo seochat tools logo