duaedf6509 2014-02-08 16:16
浏览 57
已采纳

如何使用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.

  • 写回答

3条回答 默认 最新

  • duanhao4156 2014-02-08 16:20
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥30 数字信号处理实验报告
  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题