Search code examples
phphtmleasyphp

How to increment a variable between two pages in which I loop


I have three pages, when I click on a button on the first one I go to the second one and when I click another button in the second one I go to the third one. Then, when I click on a button in the third one, I go back to the second one There is a variable that I increment every time I go to the third page but when I go back to the second page, even though I send the variable back and incremented to the second page, the variable remains the same as before I went t the third page.

page 1:

<head>
<meta charset="utf-8" />
<title> Evaluation des enseignements </title>
<link rel="stylesheet" type="text/css" name="style_telecommande.css" > 
</head>

<body>
<header>
<h1> Site de vote </h1>
<h2> Bienvenue sur le site d'évaluation des enseignements </h2>
</header>
<p> Entrez votre numéro étudiant INE </p>
<form action="page-vote.php" method="post">

<input type="text" name="numero" value="" id="num_etu" />

<label> Votre nom: </label>: <input type="text" name="name" value="" id="nom" >
<label> Votre prenom </label>: <input type="text" name="validation" value="" id="ok" />
<input type="submit" name="ok" id="entree" value="Valider" />

</form>

</body>

page 2:

<head>

<link rel= name="style-telecommande.css" type="text/css" >
<title> Telecommande de vote </title>
<meta charset="utf-8" />
</head>

<body>
<header> 
<h1> Site de vote </h1>
<h2> Télécommande de vote </h2>
</header>
<?php
if (isset($_POST["num_question"]))
{
//here is the instruction executed when I come from the third page, the variable doesn't correspond to what I sent
$question_pg_vote= max(1, min(20,(int) ($_POST["num_question"])));

echo $question_pg_vote;
echo 'yes';
}
else {
//here is the instruction executed when I come from the first page, everything works until then
    echo 'nope';
    $question_pg_vote=1;
}
//echo '<p> question numéro '.$question.' </p>';
echo'
<form action="page_validation.php" method="post">
<input type="submit" name="reponse1" value="A" />
<input type="submit" name="reponse2" value="B" />
<input type="submit" name="reponse3" value="C" />
<input type="submit" name="reponse4" value="D" />
<input type="submit" name="reponse5" value="E" />
<input type="hidden" name="numero_question" value="question_pg_vote" />
</form>
';
?>

</body>

page 3:

<?php
try
{
$bdd = new PDO('mysql:host=localhost;dbname=bdd projet s8;charset=utf8','root','');
}
catch (Exception $e) 
{
    die('Erreur : ' .$e->getMessage());
}
$question= (int) max (1,min(20, ($_POST['numero_question'])))+1;
//echo (int) max (1,min(20, ($_POST["numero_question"])));
if(isset($_POST['reponse1']))
{
$vote=$_POST['reponse1'];
}
if(isset($_POST['reponse2']))
{
$vote=$_POST['reponse2'];
}
if(isset($_POST['reponse3']))
{
$vote=$_POST['reponse3'];
}
if(isset($_POST['reponse4']))
{
$vote=$_POST['reponse4'];
}
if(isset($_POST['reponse5']))
{
$vote=$_POST['reponse5'];
}
// the 12 second lines are for adding data into my database, don't pay attention to them
/*if ($question==1)
{
$id= random_int;
}*/
$reponse = $bdd->prepare('INSERT INTO reponse(id_question,id_votant,reponse_votant) VALUES(:question1,\'1\',:reponse_votant)');
$reponse->execute(array(
'reponse_votant' => $vote
,'question1' => $question
//,'id_user' => $id 
));
echo'
<form action="page-vote.php" method="post">
<input type="submit" name="retour" value="return" />
<input type="hidden" name="num_question" value="question" />
</form>
<p> question '.$question.' </p>
//the line before displays the variable and I can see it has been incremented
';
?>
</body>

Solution

  • On the page2 you are not passing the PHP variable value

    <input type="hidden" name="numero_question" value="question_pg_vote" />
    

    should be :

    <input type="hidden" name="numero_question" value="'.$question_pg_vote.'" />
    

    Same for the page 3

     <input type="hidden" name="num_question" value="question" />
    

    should be :

    <input type="hidden" name="num_question" value="'.$question.'" />