我對PHP的知識非常有限,並會感謝任何幫助。如何使用PHP將表單值傳遞到多個頁面?
我想創建一個PHP測驗,其中每個問題在不同的頁面上。在第三頁上,對第一頁和第二頁的答案進行評分和計算。問題是我只得到最後一個答案的分數(從第二頁開始)
我試圖使用會話,但可能會搞砸了一些東西。此外,我不使用數據庫。
在第一頁(q1.php)我有這種形式
<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>
在第二頁(q2.php)我有這樣的代碼
<?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>
而對第三頁(rezultat.php)需要計算第1頁和第2頁的答案
<?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"; ?>
請幫忙,提前謝謝。