0

How I can tell in php if they answer the questions 1,2,3,5,6 with YES the let out "the answer A" if they answer the questions 1,3,5,10,12 with YES let out "the answer B" in other cases, let out "the answer C"

    <form action="questions".php" method="POST">
<p>1.Question1?</p>
<p><select> <option value="YES">YES</option> <option value="NO">NO</option> </select></p>
<p>2.Question2?</p>
<p><select> <option value="YES">YES</option> <option value="NO">NO</option> </select></p>
<p>3.Question3?</p>
<p><select> <option value="YES">YES</option> <option value="NO">NO</option> </select></p>
<p>4.Question4?</p>
<p><select> <option value="YES">YES</option> <option value="NO">NO</option> </select></p>
<p>5.Question5?</p>
<p><select> <option value="YES">YES</option> <option value="NO">NO</option> </select></p>
<p>6.Question6?</p>
<p><select> <option value="YES">YES</option> <option value="NO">NO</option> </select></p>
7.Question7?
<p><select> <option value="YES">YES</option> <option value="NO">NO</option> </select></p>
8.Question8?
<p><select> <option value="YES">YES</option> <option value="NO">NO</option> </select></p>
9.Questione9?
<p><select> <option value="YES">YES</option> <option value="NO">NO</option> </select></p>
10.Question10?
<p><select> <option value="YES">YES</option> <option value="NO">NO</option> </select></p>
11.Question11?
<p><select> <option value="YES">YES</option> <option value="NO">NO</option> </select></p>
<input name="submit" type="submit" value="submit" /> </form>
3
  • There's a typo in your question. What is the current code for questions.php? Commented Jul 6, 2015 at 14:08
  • An questions about the provided answers or did one work for you? Commented Jul 7, 2015 at 5:04
  • for chris85 - everything is clear. This is the best solution. Thank you so much Commented Jul 8, 2015 at 12:57

2 Answers 2

1

Here's a sample of how this could be done using selects. I think radio buttons would be a better usage here.

<form method="POST" action="questions.php">
            <p>1.Question1?</p>
    <p>
        <select name="questions[1]">
            <option value="YES">YES</option>
            <option value="NO">NO</option>
        </select>
    </p>
    <p>2.Question2?</p>
    <p>
        <select name="questions[2]">
            <option value="YES">YES</option>
            <option value="NO">NO</option>
        </select>
    </p>
    <p>3.Question3?</p>
    <p>
        <select name="questions[3]">
            <option value="YES">YES</option>
            <option value="NO">NO</option>
        </select>
    </p>
    <p>4.Question4?</p>
    <p>
        <select name="questions[4]">
            <option value="YES">YES</option>
            <option value="NO">NO</option>
        </select>
    </p>
    <p>5.Question5?</p>
    <p>
        <select name="questions[5]">
            <option value="YES">YES</option>
            <option value="NO">NO</option>
        </select>
    </p>
    <p>6.Question6?</p>
    <p>
        <select name="questions[6]">
            <option value="YES">YES</option>
            <option value="NO">NO</option>
        </select>
    </p>
    7.Question7?
    <p>
        <select name="questions[7]">
            <option value="YES">YES</option>
            <option value="NO">NO</option>
        </select>
    </p>
    8.Question8?
    <p>
        <select name="questions[8]">
            <option value="YES">YES</option>
            <option value="NO">NO</option>
        </select>
    </p>
    9.Questione9?
    <p>
        <select name="questions[9]">
            <option value="YES">YES</option>
            <option value="NO">NO</option>
        </select>
    </p>
    10.Question10?
    <p>
        <select name="questions[10]">
            <option value="YES">YES</option>
            <option value="NO">NO</option>
        </select>
    </p>
    11.Question11?
    <p>
        <select name="questions[11]">
            <option value="YES">YES</option>
            <option value="NO">NO</option>
        </select>
    </p>
    <input name="submit" type="submit" value="submit" />
</form>

and then to process on the PHP side...(I'm also not sure how your conditions work, here is one way it could be done.)

foreach($_POST['questions'] as $key => $question) {
        switch($key) {
            case 1:
            case 2:
            case 3:
            case 5:
            case 6:
                if($question == 'YES') {
                    echo 'the answer A';
                } else {
                    echo 'the answer C';
                }
            break;
            case 4:
            case 7:
            case 8:
            case 9:
            case 10:
            case 11:
                if($question == 'YES') {
                    echo 'the answer B';
                } else {
                    echo 'the answer C';
                }
            break;
        }
    }

Output:

Question #1=YES<br />Question #2=NO<br />Question #3=NO<br />Question #4=NO<br />Question #5=NO<br />Question #6=NO<br />Question #7=NO<br />Question #8=NO<br />Question #9=NO<br />Question #10=NO<br />Question #11=NO<br />
Sign up to request clarification or add additional context in comments.

1 Comment

You could put a name in those brackets, like name="questions[1]", name="questions[two]", name="questions[3]" and probably might be a good idea to add selected to the first option.
0

You have to give a name to the select for example:

echo "<select name='questionOne'>";
echo "<option value='YES'>YES</option>";
echo "<option value='NO'>NO</option>";
echo "</select><br/>";
echo "<select name='questionTwo'>";
echo "<option value='YES'>YES</option>";
echo "<option value='NO'>NO</option>";
echo "</select><br/>";

Then just do a conditional test:

if($_POST['questionOne'] == 'YES')
{
//do something
}
elseif($_POST['questionOne'] == 'NO')
{
//do something else
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.