Barebones PHP survey framework with zero client and server side dependencies
Barebones PHP survey framework with zero client and server side dependencies., (*2)
Multi-page, branching survey framework that doesn't require JavaScript nor cookies client-side, and also doesn't need sessions or an SQL back-end server-side., (*3)
At each page, hidden form variables persist answers previously given, until the form is completed where results are saved sequentially in a plain CSV file., (*4)
See the included example pages and the NanoSurvey class' documentation below for usage., (*5)
You can just include NanoSurvey.php
directly in your project or use
Composer:, (*6)
$ composer require vphantom/nanosurvey
Instantiate the NanoSurvey
class, which will process $_REQUEST
's a
, p
and x
CGI variables to situate itself. Invoke page()
to display the current page inside a <form>
block., (*7)
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Survey</title> </head> <body>Survey
page(); ?> </body> </html>
class NanoSurvey
NanoSurvey class, (*8)
public function __construct($filename, $savePartial = false)
Initialize a survey, (*9)
The first CSV column is the timestamp when the responses are saved, the rest are each response in order., (*10)
If $savePartial is set true, each time a participant submits a page, a new CSV line is appended with all answers obtained so far. In this mode, 2 extra columns are prepended after the timestamp: a unique participant ID and a page number. This makes it easy to keep only the most complete response from each participant, including those who did not reach the end of the survey., (*11)
If $savePartial is omitted or false, only complete surveys will be saved., (*12)
Parameters:, (*13)
$filename
— string — Path and name for the CSV results file$savePartial
— bool|null — Save incomplete rows during progresspublic function previousAnswer($id)
Get previous answer, if one was given, (*14)
Answers count from 1, radios share the same ID, other types increment it each time., (*15)
The safest way to refer to a previous page's answer is to build those pages first and view their HTML source client-side., (*16)
Parameters:, (*17)
$id
— int — Serial number of the previous answerReturns: mixed
— Contents of answer, (*18)
public function progressVars()
Save current progress in hidden FORM variables, (*19)
Returns: string
— HTML with hidden inputs, (*20)
public function newQuestion($type = 'normal')
Begin a new question, (*21)
Initializes internal data. Every question MUST start with a call to this function., (*22)
Parameters:, (*23)
$type
— string — Normal by default, specify 'radio' for a radio groupReturns: void, (*24)
public function endQuestion()
End current question, (*25)
Finalizes internal data. Every question MUST end with a call to this function., (*26)
Returns: void, (*27)
public function radioCheckbox($value, $default = false)
Radio (one of many) checkbox, (*28)
Parameters:, (*29)
$value
— mixed — Internal value to save if box is checked$default
— bool|null — Set true to make this the default inputReturns: string
— HTML checkbox, (*30)
public function checkbox($value)
Checkbox (multiple possible), (*31)
Parameters:, (*32)
$value
— mixed — Internal value to save if box is checkedReturns: string
— HTML checkbox, (*33)
public function textbox($placeholder = "please specify", $size = 0)
Single line text input box, (*34)
Parameters:, (*35)
$placeholder
— string|null — Text to display inside box when it is empty$size
— int|null — How many characters wide the input should be?Returns: string
— HTML input box, (*36)
public function beginSelectOne()
Select one within many, (*37)
Differs from radio buttons in that a single drop-down is displayed., (*38)
Returns: string
— HTML select initialization, (*39)
public function endSelectOne($first, $last)
Close select one within many, (*40)
If optional arguments $first and $last are specified, numeric options are automatically created and appended for each possibility. You may still wish to create one "none selected" option with an empty value if you'd like before calling this., (*41)
Parameters:, (*42)
$first
— int|null — First option$last
— int|null — Last option (max: $first + 500)Returns: string
— HTML select finalization, (*43)
public function placeholder()
Placeholder answer, (*44)
When only a specific question or answer choice should be skipped conditionally, instead of an entire page, display these placeholders where the skipped answers would've been. This preserves the integrity of the answer count at all times., (*45)
Returns: string
— HTML hidden input, (*46)
public function submitButton($label = "Continue")
Create submit button for next page, (*47)
Parameters:, (*48)
$label
— string — HTML content of button (Default: "Continue")Returns: string
— HTML, (*49)
public function page()
Display the current/next page, (*50)
Pages are expected to be sequential, from "page-0.inc", "page-1.inc", etc. until the final page which invokes endSurvey()., (*51)
Variable $survey is available in these pages, representing $this instance of NanoSurvey., (*52)
Returns: string
— HTML of the page, (*53)
public function skipPage()
Skip the current page, display next one instead, (*54)
If, in a page, you assess that it should be skipped (i.e. based on prior answers), call this method., (*55)
Returns: void, (*56)
public function endSurvey()
Terminate survey, saving to CSV if necessary, (*57)
Returns: void, (*58)
MIT License, (*59)
Copyright (c) 2017 Stéphane Lavergne https://github.com/vphantom, (*60)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:, (*61)
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software., (*62)
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE., (*63)