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条)

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘