Creating a Questionnaire, multiple questions with multiple routes

Hi there,
What would be best practice when attempting to build a questionnaire, with multiple choice answers where the next question will be different on answers selected?

I’m thinking how would I test this but with multiple routes, it be difficult to determine the route so i somehow need to keep a record of the questions being asked…

I’m thinking of creating a session (or sessions?) and then with each button pressed have the title of the question along with the answer stored in a session?

Any pointers would be helpful, cheers.

Thanks
Darren

Well, first, save everything in a database to make it easy to process queries.
Next, if you know all the questions and answers, simply create a list of each with an index and a level number for the question level. You would be able to query level the level one question and have a list of possible answers and indexes where each question would take the use to in the level two questions.
Then, just loop thru to the end of questions.
Is this for school? Most people learning programming think about the code starting out. They do not think about the data until they get started with the code. I suggest you layout your data structure first and as you define it, you will see ways to mark where you are and where to head next.
Hope this helps.

Hi, I did actually plan it out and all the possible routes into a spreadsheet. So I’ve mapped the paths. I was trying to see if I could do it without having to use a database, store the answers in a variable and then at the end present them…like, “this is what you’ve said…”

Keep your questions in an indexed array, something like this:

$questions = [
    // Index is the question ID, used later
    1 => [
        'question' => 'What is your favourite colour?',
        'answers' => [
            1 => [
                'answer' => 'red',
                'next_question' => 2
            ],
            2 => [
                'answer' => 'green',
                'next_question' => 3
            ],
        ]
    ],
    // next question...
];

Your user starts on question 1. When they submit an answer, you look up that answer for that question to find out next_question.

Right. I think I’ve messed this project up then, ha!

So what I’ve done in my PHP wisdom is to have each question as an include, and when the user submits the form (along with the answer & i’m using $_POST) the next line looks at the submit variable and answer and then displays that include…

The answers are stored in a session but i think because im using the same form, it over writes it.

I’ll show you the code but don’t laugh or atleast type laughter, ha…

<?php  session_start(); ?>
<?php include("includes/a_config.php");?>
<!DOCTYPE html>
<html>
<head>
<?php $CURRENT_PAGE = "Contact"; ?>
	<?php include("includes/head-tag-contents.php");?>
</head>
<body>
<?php
//create sessions	- my intention here was to collect their answers
$_SESSION['postcode'] = $_POST['postcode'];
$_SESSION['type_of_fuel'] = $_POST['type_of_fuel'];
$_SESSION['type_of_boiler'] = $_POST['type_of_boiler'];
$_SESSION['boiler_location'] = $_POST['boiler_location'];
$_SESSION['different_location'] = $_POST['different_location'];
$_SESSION['new_boiler_location'] = $_POST['new_boiler_location'];
$_SESSION['property_description'] = $_POST['property_description'];
$_SESSION['no_of_bedrooms'] = $_POST['no_of_bedrooms'];
$_SESSION['no_of_radiators'] = $_POST['no_of_radiators'];
$_SESSION['no_of_baths'] = $_POST['no_of_baths'];
$_SESSION['no_of_showers'] = $_POST['no_of_showers'];
$_SESSION['electric_shower'] = $_POST['electric_shower'];
$_SESSION['flue_location'] = $_POST['flue_location'];
$_SESSION['angle_flue_location'] = $_POST['angle_flue_location'];
$_SESSION['close_to_ground'] = $_POST['angle_flue_location']; 	
$_SESSION['close_to_property'] = $_POST['close_to_property']; 	
$_SESSION['close_to_structure'] = $_POST['close_to_structure']; 
$_SESSION['30cm_close'] = $_POST['30cm_close']; 
$_SESSION['convert_to_combi'] = $_POST['convert_to_combi'];
	


	

	
;?>
<?php include("includes/design-top.php");?>

<div class="container" id="main-content">


<h1 class="text-center">Help us to provide the best quote for you</h1>
    <div id="quote_container" class="d-flex justify-content-center text-center">

		<div style="position: fixed;top:0;left:0;background: white;text-align: left;padding: 1.5em;display:none;">
<?php
		echo '<ol>';
		echo '<li>What is their postcode <b>' .$_SESSION['postcode'].'</b></li>';
		echo '<li>What kind of fuel does your boiler use? <b>' .$_SESSION['type_of_fuel'].'</b></li>';
		echo '<li>What type of boiler do you currently have? <b>' .$_SESSION['type_of_boiler'].'</b></li>';
		echo '<li>Where is your boiler currently boiler? <b>' .$_SESSION['boiler_location'].'</b></li>';
		echo '<li>Do you want the boiler in a different location? <b>' .$_SESSION['different_location'].'</b></li>';
		echo '<li>Which best describes your property? <b>' .$_SESSION['property_description'].'</b></li>';
		echo '<li>A Visit is needed to be arranged</li>';
		echo '<li>How many bedrooms do you have? <b>' .$_SESSION['no_of_bedrooms'].'</b></li>';
		echo '<li>How many radiators? <b>' .$_SESSION['no_of_radiators'].'</b></li>';
		echo '<li>How many bathtubs do you have, or plan to have? <b>' .$_SESSION['no_of_baths'].'</b></li>';
		echo '<li>How many separate showers do you have, or plan to have in future? <b>' .$_SESSION['no_of_showers'].'</b></li>';
		echo '<li>Where does your flue come out? <b>' .$_SESSION['flue_location'].'</b></li>';
		echo '<li>Is your flue on a sloped roof or flat roof? <b>' .$_SESSION['angle_flue_location'].'</b></li>';
		echo '<li>Where on the roof is it positioned? <b>' .$_SESSION['roof_position'].'</b></li>';
		echo '<li>Is it square or round? <b>' .$_SESSION['flue_shape'].'</b></li>';
		echo '<li>How close to the ground is your flue? <b>' .$_SESSION['close_to_ground'].'</b></li>';
		echo '<li>How close to another property is your flue? <b>' .$_SESSION['close_to_property'].'</b></li>';
		echo '<li>Is the flue under a carport balcony or other structure? <b>' .$_SESSION['close_to_structure'].'</b></li>';
		echo '<li>Is the flue 30cm or more from a door or window? <b>' .$_SESSION['30cm_close'].'</b></li>';
		echo '<li>Do you want to convert to a combi boiler?<b>' .$_SESSION['convert_to_combi'].'</b></li>';
		echo '</ol>';

;?>
</div>


    <form action="#quote_container" method="post">

    <!-- STEP 1 - Would the Customer like to buy online or arrange a visit -->
    

    
    <?php if(empty($_POST)){ ?>
      <p>Would you like to <a href="">buy online</a> or arrange a <a href="">home visit</a>?</p>
      <div class="form-group">
    <!-- <button type="subit" class="btn btn-primary" name="online_quote" value="continue_online">Buy online</button> -->
    <button type="submit" class="btn btn-primary" name="online_quote" value="continue_home">Start Quote</button>
    </div>

   

<!-- Question 1 Enter Post Code -->
<?php }elseif ($_POST['online_quote'] === 'continue_home'){ ?>
<?php include("q1.php"); ?>
<?};?>
		
<!-- Question 2 What fuel does your boiler use -->
<?php if ( ($_POST['online_quote'] === 'continue_home_1') && (isset($_POST['postcode']))){ ?>
<?php include("q2.php"); ?>
<?};?>
		
<!-- Question 3 What type of boiler do you currently have -->
<?php if ($_POST['online_quote'] === 'continue_home_2'){ ?>
<?php include("q3.php"); ?>
<?};?>			

<!-- Question 4 Where is your currently fitted? -->
<!-- This will only appear if user selects 'combi' -->		
<?php if ( (($_POST['online_quote'] === 'continue_home_3') && ($_POST['type_of_boiler'] == 'Combi')) ||
 (($_POST['online_quote'] === 'continue_home_21') && ($_POST['How close to the ground is your flue

_to_combi'] == 'No, to converting to Combi Boiler'))){ ?>
<?php include("q4.php"); ?>
<?};?>			

<!-- Question 21 Do you want to convert to a combi boiler -->	
<!-- This will only appear if user selects 'system' -->		
<?php if ( ($_POST['online_quote'] === 'continue_home_3') && ($_POST['type_of_boiler'] == 'System')){ ?>
<?php include("q21.php"); ?>
<?};?>
		
<!-- Question 21 Do you want to convert to a combi boiler -->
<!-- This will only appear if user selects 'Heat Only' -->		
<?php if ( ($_POST['online_quote'] === 'continue_home_3') && ($_POST['type_of_boiler'] == 'Heat Only')){ ?>
<?php include("q21.php"); ?>
<?};?>	
		
<!-- Question 21 Do you want to convert to a combi boiler -->
<!-- This will only appear if user selects 'Back Boiler' -->		
<?php if (( ($_POST['online_quote'] === 'continue_home_3') && ($_POST['type_of_boiler'] == 'Back Boiler'))||
		  (($_POST['online_quote'] === 'continue_home_5')&& ($_POST['different_location'] == 'yes, a different location'))||
		 (($_POST['online_quote'] === 'continue_home_21')&& ($_POST['convert_to_combi'] == 'Yes, How close to the ground is your flue

 to Combi Boiler'))){ ?>		
<?php include("q7.php"); ?>
<?};?>
		
		
<!-- Question 5 Do you want your boiler in a different place? -->		
<?php if ($_POST['online_quote'] === 'continue_home_4'){ ?>
<?php include("q5.php"); ?>
<?};?>
		
<!-- Question 6 Which best describes your property -->
		<!-- Get here by clicking 'NO' on question 5 -->
<?php if (($_POST['online_quote'] === 'continue_home_5')&& ($_POST['different_location'] == 'no, the same location')){ ?>		
<?php include("q6.php"); ?>
<?};?>	
		
<!-- Question 8 How Many Bedrooms do you have? -->
<?php if($_POST['online_quote'] === 'continue_home_6'){ ?>		
<?php include("q8.php"); ?>
<?};?>	
		
<!-- Question 9 How Many Radiators do you have? -->
<?php if($_POST['online_quote'] === 'continue_home_8'){ ?>		
<?php include("q9.php"); ?>
<?};?>
		
<!-- Question 10 How Many bathtubs do you have, or plan to have in the future? -->
<?php if($_POST['online_quote'] === 'continue_home_9'){ ?>		
<?php include("q10.php"); ?>
<?};?>	
		
<!-- Question 11 How Many showers do you have, or plan to have in the future? -->
<?php if($_POST['online_quote'] === 'continue_home_10'){ ?>		
<?php include("q11.php"); ?>
<?};?>
		
<!-- Question 12 Where does your flue come out? -->
<?php if($_POST['online_quote'] === 'continue_home_11'){ ?>		
<?php include("q12.php"); ?>
<?};?>
	
<!-- Question 13 Is your Flue on a sloped roof or flat roof?? -->
<?php if(($_POST['online_quote'] === 'continue_home_13') && ($_POST['flue_location']== 'from the roof')){ ?>		
<?php include("q13.php"); ?>
<?};?>
	
<!-- Question 13 Is your Flue on a sloped roof or flat roof?? -->
<?php if(($_POST['online_quote'] === 'continue_home_14') && ($_POST['angle_flue_location']== 'sloped')){ ?>		
<?php include("q14.php"); ?>
<?};?>		
		
<!-- Question 16 Is it square or round? -->
<!-- You get here by clicking 'Wall' on Q5 -->		
<?php if(($_POST['online_quote'] === 'continue_home_13') && ($_POST['flue_location']== 'from the wall')){ ?>		
<?php include("q16.php"); ?>
<?};?>
		
<!-- Question 15 PROVIDE A QUOTE -->		
<?php if(($_POST['online_quote'] === 'continue_home_14') && ($_POST['roof_position']== 'roof position highest two thirds')
		|| ($_POST['online_quote'] === 'continue_home_14') && ($_POST['roof_position']== 'roof position lowest third')
		|| ($_POST['online_quote'] === 'continue_home_14') && ($_POST['angle_flue_location']== 'flat')
		 || ($_POST['online_quote'] === 'continue_home_20')
		
		){ ?>		
<?php include("q15.php"); ?>
<?};?>			

<!-- Question 17 How close to the ground is your flue -->
<!-- You get here by clicking 'Wall' on Q5 -->		
<?php if($_POST['online_quote'] === 'continue_home_16'){ ?>		
<?php include("q17.php"); ?>
<?};?>
		
<!-- Question 18 How close to another property is your flue? -->		
<?php if($_POST['online_quote'] === 'continue_home_17'){ ?>		
<?php include("q18.php"); ?>
<?};?>
		
<!-- Question 19 Is the flue under a carport, balcony or other structure? -->		
<?php if($_POST['online_quote'] === 'continue_home_18'){ ?>		
<?php include("q19.php"); ?>
<?};?>			

<!-- Question 20 Is the flue 30cm or more from a door or window? -->		
<?php if($_POST['online_quote'] === 'continue_home_19'){ ?>		
<?php include("q20.php"); ?>
<?};?>	
		
</form>
	</div>
	</div>
<?php include("includes/footer.php");?>

</body>
</html>

It’s not a school project, i wanted to help a buddy out with something and attempting to help…ha

You could probably get that approach to work, but you’re going to trip yourself up a lot trying to keep the routes straight.

First problem I see is that you’re reading from $_POST for every request; if you’re not showing all the questions every time, a lot of these are going to be blank. Once you’ve stored the user’s answer, you shouldn’t be writing to that answer again.

You’re right because what I did was to create a visible div to fix to the screen as i went through the questions, the answers i ticked would be visible then moved, then as i move it would be erased…

…then to solve this i thought would it be best to submit the answers to the database and then have a script to just call the answers…

All i know is that i’ve messed it up and i need to back and start on a smaller part, just test that and then move on…

…i so wish i started learning PHP earlier in my bloody life!

Thanks for you input and pointers my friend

The purpose of the session variables are to remember the current state of the process. They are essentially a shopping cart for collecting question/answer information. The item ids in the cart should be the question ids and the stored values should either be the chosen answer id, for multiple choice questions, or quantities. The post method form processing code should be above the start of the html document. The form processing code should detect if a post method form has been submitted, trim then validate all the inputs, then Create, Update, or Delete items in the cart, i.e. the CUD of the CRUD design pattern. After successfully processing the form submission, you should execute a redirect to the exact same url of the current page to cause a get request. The ‘get method business logic’ would then determine what output to produce on the page, based on the current state of the process from the session data.

Sponsor our Newsletter | Privacy Policy | Terms of Service