Hey all! Before I even ask any detailed questions just curious if it is even possible to create Skip Logic / Conditional Branching for a Form using PHP.

Example: (If user answers "No" to question 1, then question 3 appears...If user answers "Yes" to question 1, then question 2 appears.) Anyone have any experience creating skip logic in a form using PHP?

Thanks in advance for you help!

    It's possible, though cumbersome. You'd need to submit the form after every choice you want this behaviour.
    Doing it with javascript is simple and saves the user unnecessary waiting on page loads.

      johanafm;10930417 wrote:

      ...Doing it with javascript is simple and saves the user unnecessary waiting on page loads.

      Though it does, of course, require that the user have JavaScript available and enabled. Admittedly these days that is almost always the case, but still not 100% (such as if I'm surfing on my Kindle 2 in "basic" web mode). If you do go with the JS approach, be sure to either make use of the <noscript> tag to notify the user that JS is required; or else if you have the time, also code a fall-back mechanism that does not require JS (e.g.: doing it on the server via a sequence of forms), then have your JavaScript override that behavior with its own.

        Thank you folks so much. I'll definitely look into the JavaScript approach. Are there any tutorials either of you could direct me to? I've been searching the without much luck. Thanks Again!

          Two good links on Mozilla's page on javascript: DOM reference and Core reference.
          What you need is pretty much:
          onchange event for your control(s)
          document.getElementById to get the control you want to show next
          element.style.visibility / element.style.display - or you could create the elements on the fly with document.createElement and then use appendChild.

          For tutorials, you might have a look at http://www.w3schools.com/js/

            10 days later

            OK...so I think that I've changed my mind. Unfortunately, I have very limited knowledge of JavaScript and have a little to no luck finding tutorials for this specific function. I am much more comfortable with PHP so I therefore think the PHP solution would work better for me. Anyone, have any ideas of what a PHP script like this (mentioned above) might look like?

            I'm assuming it would start out something like this (assuming the response to the question "age" is "NO" the user can't continue with the survey...assuming the response to the question "age is "YES" the user continues as normal):

            <?php
            
            $age=$_POST['age'];
            
            if ($age=="no")
            {header("Location: discontinue.html");
            }
            elseif ($age=="YES")
            {header("Location: continue.html");
            }
            ?>

              I believe the easiest approach would be to keep track of the progress with a $progress variable. Keept it as a session variable or set a cookie, or send it along in a hidden input field. Also, I wouldn't use the location header for this.

              Can be put in another file and "required"

              function handleForm1() {
              	// input validation and sanitizing as needed
              	// ...
              	if (!$validationOK) {
              		return getForm1();
              	}
              
              if (isset($_POST['age']) && $_POST['age'] == 'yes')
              	return getForm2();
              else
              	return getForm3();
              }
              function getForm2() {
              	$str = '<form ...>';
              	// using hidden input to deal with progress. This is the second form.
              	$str .= '<input type="hidden" name="progress" value="2" />';
              	// ...
              	$str .= '</form>';
              	return $str;
              }
              function getForm3() {
              	// ...
              	$str .= '<input type="hidden" name="progress" value="3" />';
              	// ...
              	return $str;
              

              target file of all the forms' action attribute

              if (empty($_POST['progress']) || $_POST['progress'] == 1) {
              	$nextForm = handleForm1();
              }
              else if ($_POST['progress'] == 2) {
              	$nextForm = handleForm2();
              }
              // ...
              else {
              	$nextForm = getForm1();
              }
              
              echo $nextForm;
              

              And if you need to carry over data from one form to the other, send the saniztized user input along as either one array parameter or several separate parameters in the call to the getFormN() functions.
              Especially in the case when validation wasn't ok (some info not filled out for example), you would want to
              1. Display all previous user input
              2. Inform the user of what's missing

                Write a Reply...