简体   繁体   中英

Form with radio buttons + logic

I'm trying to create a form (kind of like a quiz) that uses radio buttons and logic to generate and display different diagnostic results. Here is what I have for some of the form structure (there are many more options and the logic is more weighty in the real version):

  <form action='index.php' method='post'>
  Q1: Does Patient experience Symptom X?
  <br/>
  <label><input type="radio" name="Q1" value="Q1Y"/> Yes</label> &nbsp;
  <label><input type="radio" name="Q1" value="Q1N"/> No</label>
  <br/>
  <br/>

  Q2: Does Patient experience Symptom Y?
  <br/>
  <label><input type="radio" name="Q2" value="Q2Y"/> Yes</label> &nbsp;
  <label><input type="radio" name="Q2" value="Q2N"/> No</label>
  <br/>
  <br/>

  Q3: Does Patient experience Symptom Z?
  <br/>
  <label><input type="radio" name="Q3" value="Q3Y"/> Yes</label> &nbsp;
  <label><input type="radio" name="Q3" value="Q3N"/> No</label>
  <br/>
  <br/>

  <input type="submit" name="formSubmit" value="See Result"/>
  <br/>

How do I set up the rest of the logic such that:

If (Q1Y && Q2Y){ 
Diagnosis = A; 
}

If (Q2Y && Q3Y){ 
Diagnosis = B; 
}

Ideally, the diagnosis would store in a variable then show on the current page when the user clicks submit on the form.

if(($_POST['Q1']=='Q1Y') && ($_POST['Q2']=='Q2Y'))
{
// Your Logic
}
if(($_POST['Q2']=='Q2Y') && ($_POST['Q3']=='Q3Y'))
{
// Your Logic
}

Use nested objects to hold the results of all the combinations of answers.

var allDiagnosis = {
    Q1Y: {
        Q2Y: {
            Q3Y: 'A',
            Q3N: 'B'
        },
        Q2N: {
            Q3Y: 'C',
            Q3N: 'D'
        },
    Q1N: {
        Q2Y: {
            Q3Y: 'E',
            Q3N: 'F'
        },
        Q2N: {
            Q3Y: 'G',
            Q3N: 'H'
        }
    }
}

Then get the value of each input, and do:

var diagnosis = allDiagnosis[value1][value2][value3];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM