'Questions 'funnel' based on previous question's answers

I am starting to build out a feature similar to a question funnel, and I want to avoid a quiz/question plugin if possible as they're a bit hacky.

Basically it'd work that there is an initial question, and depending on that answer, that will choose the 2nd question, and then depending on that answer will either show 1 of 2 questions and so on, until the final question and then will show some content to the user.

I was thinking of using ACF Pro and using a 'parent question' approach, but this doesn't allow me to show based on the answer, only the parent question.

  • Questions based on the answers of the parent question
  • If no child question(s), show the content of the answer above. Eg if 'not pregnant', show content, but if 'pregnant', then show next questions.

Thoughts? Any help appreciated.



Solution 1:[1]

If you want to have your form without page reload, then you should, obviously, use javaScript/jQuery. However, it seems you're looking for a PHP solution. You can achieve this by sending and getting globals through form. Depending on that, you can do your logic. Simple example:

<form action="/quiz-page-2" method="get">
    <input type="radio" id="html" name="fav_language" value="HTML">
    <label for="html">HTML</label><br>
    <input type="radio" id="css" name="fav_language" value="CSS">
    <label for="css">CSS</label><br>
    <input type="radio" id="javascript" name="fav_language" value="JavaScript">
    <label for="javascript">JavaScript</label>
    <button type="submit">Next page</button>
</form>

On the next page, quiz-page-2, you check for the answer

<?php
if($_GET['fav_language'] === 'HTML')
     // Another question...
if($_GET['fav_language'] === 'CSS')
     // Another question...
if($_GET['fav_language'] === 'JavaScript')
     // Another question...
    

If you will use GET method, answers will be visible in the URL, but if you want to keep URL clean, you can use POST method, then form method would be post, and you get your variables like this $_POST['fav_language'].

EDIT:

Option 2, without hard coding. It would require to install ACF Multistep plugin, there might be more plugins with similar approach. You would start the Field group with the step field enter image description here And for the 2 question you would choose conditional logic, if some value, then show this field. enter image description here Live example

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1