如何使用PHP将表单值传递给多个页面?

I have very limited knowledge in php and will appreciate any help.

I am trying to create a php quiz, where every question is on different page. On the third page the answers from 1st and 2nd page are scored and calculated. The problem is that I only get score for last answer (from 2nd page)

I tried to use Sessions but probably messed up something. Also I don't use data base for this.

On first page (q1.php) I have this form

 <form action="q2.php" method="post"><li>
    <h3>Koji ste pol?</h3>

    <div>
    <input type='radio' name='answer1' id='answer1' value='A' />
    <label for='answer1A'>Muško</label>
    </div>

    <div>
    <input type='radio' name='answer1' id='answer1' value='B' />
    <label for='answer1B'>Žensko</label>
    </div>
  </li><input type="submit" value="posalji" /></form>

On second page (q2.php) I have this code

 <?php
 session_start();
 $_SESSION['answer1'] = $_POST['answer1'];
 ?>

 <form action="rezultat.php" method="post"><li>
    <h3>Da li ste zaposleni?</h3>

    <div>
    <input type='radio' name='answer2' id='answer2' value='A' />
    <label for='answer2A'>Da</label>
    </div>

    <div>
    <input type='radio' name='answer2' id='answer2' value='B' />
    <label for='answer2B'>Ne</label>
    </div>
  </li><input type="submit" value="posalji" /></form>

And on third page (rezultat.php) the answers from 1st and 2nd page need to be calculated

<?php
 session_start();
 $_SESSION['answer1'] = $_POST['answer1'];
 $_SESSION['answer2'] = $_POST['answer2'];
 ?>

Vaš rezultat je: 

<?php
$answer1= $_POST['answer1'];
$answer2= $_POST['answer2'];

$score = 0;

if ($answer1 == "A"){$score += 5;}
if ($answer1 == "B"){$score += 4;}

if ($answer2 == "A"){$score += 1;}
if ($answer2 == "B"){$score += 5;}

echo "$score"; ?>

Please help, thank you in advance.

On your third page (rezultat.php), you should remove this line:

$_SESSION['answer1'] = $_POST['answer1'];

It's no longer needed as you already assigned value on $_SESSION['answer1'] from second page (q2.php). What you're currently doing is assigning a null or empty value on $_SESSION['answer1'] since $_POST['answer1'] never existed in second page (q2.php).

Then replace this:

$answer1= $_POST['answer1'];
$answer2= $_POST['answer2'];

to

$answer1= $_SESSION['answer1'];
$answer2= $_SESSION['answer2'];

You might want to add some validation layer on your $_POST before assigning it to the $_SESSION.

$_POST['answer1'];

isn't available on rezultat.php, so right now you're overwriting the answer with non-existing POST data. Before reading a $_POST[] field, run a quick check to see if it's actually available

 if(isset($_POST['answer1'])){}

That would have given you a clue that answer1 isn't available on the final pae.

"answer1" field not available in second page(q2.php), so you cann't use $_POST['answer1'].

Use below corrected script!

     <?php
      session_start();
      $_SESSION['answer2'] = $_POST['answer2'];
     ?>

    Vaš rezultat je: 

    <?php
    $answer1= $_SESSION['answer1'];
    $answer2= $_SESSION['answer2'];

    $score = 0;

    if ($answer1 == "A"){$score += 5;}
    if ($answer1 == "B"){$score += 4;}

    if ($answer2 == "A"){$score += 1;}
    if ($answer2 == "B"){$score += 5;}

    echo "$score"; ?>