在会话中存储单选按钮值 - PHP

How can I create a form with 3 radio button and a button to click "submit" and will be re-directed to the next page showing the amount of each radio button chose using session.

Example:

Choose one of Radio Button A, Radio Button B, Radio Button C

Submit

Brings to next page showing the amount of each radio button chosen,

  • Radio Button A - 5 times
  • Radio Button B - 2 times
  • Radio Button C - 0 times.

So far I am only able to do it using database, what if I would want to use session?

Update: My Current code I have that only shows the radio button I selected

file1.php

session_start();
$_SESSION['choice'] = $choiceVal;

<form method="get" action="file2.php">
    <input type="radio" name="choice" value="R1"> R1<br>
    <input type="radio" name="choice" value="R2"> R2<br>
    <input type="radio" name="choice" value="R3"> R3<br>
    <input type="submit">
</form>

file2.php

session_start();
$choiceVal = $_GET['choice'];
echo "Your registration is: ".$choiceVal.".";

<p><a href="file1.php">Back to main page</a>

If you add your code, I'll be able to add a more detail answer.

Change test2.php to:

<?php

session_start();

if (!isset($_SESSION['count'])) {
  $_SESSION['count'] = [
    'A' => 0,
    'B' => 0,
    'C' => 0,
  ];
}

$option = isset($_GET['choice']) ? $_GET['choice'] : false;

if ($option) {
  $_SESSION[count][$option]++;
}

echo '<ul>';
foreach ($_SESSION['count'] as $key => $value) {
  echo '<li>Radio Button ' . $key . ' - '. $value .' times.</li>';
}
echo '</ul>';

Your code for file2.php

<?php 
session_start();
if(isset($_GET['choice'])){//Check the get value.
    if(!isset($_SESSION['click'])){//Check the session exist or not. if not define new array with 0 to all R1,R2,R3.
        $_SESSION['click'] = array('R1'=>0,'R2'=>0,'R3'=>0);
    }
    // Assign the clicked button value to same key's array with +1;
    $_SESSION['click'][$_GET['choice']] = ($_SESSION['click'][$_GET['choice']]+1);
    echo "Your registration is: ".$_SESSION['click'][$_GET['choice']].'<p><a href="q.html">Back to main page</a>';
}
// Print your click count
echo '
<ul>
    <li>Radio Button A - '.$_SESSION['click']['R1'].' times</li>
    <li>Radio Button B - '.$_SESSION['click']['R2'].' times</li>
    <li>Radio Button C - '.$_SESSION['click']['R3'].' times</li>
</ul>';
?>