I have a form in index.php and it is processed in my process.php. I wanted to hide the form in index.php after submit and change it into the confirmation message. But, my code's not working how i wanted it to work. Here's my code:
This is my index.php(the session counter is working but it is staying on the form after clicking submit):
<?php session_start();?>
<html>
<body>
<main>
<?php
if(isset($_POST['name_entered'])){
if($_SESSION['counter'] <= 10 && $_SESSION['counter'] > 0) {
//$_SESSION['counter'] = $_SESSION['counter'] 1;
?>
<h2>Welcome Customer!</h2>
<p>We are giving away <span >free coupons</span> as token of appreciation</p>
<section>
<h4>50% discount</h4>
<h1 id="random_num"><?= rand(1000000,9999999);?></h1>
<form action="process.php" method="post">
<input type="submit" name="reset_btn" value="Reset Count">
<input type="submit" name="claim_again_btn" value="Claim Again">
</form>
</section>
<?php } else { ?>
<h2>Welcome Customer!</h2>
<p>We are giving away <span >free coupons</span> as token of appreciation</p>
<section>
<h3>Sorry!</h3>
<h1>Unavailable!</h1>
<form action="process.php" method="post">
<input type="submit" value="Reset Count" name="reset_btn">
</form>
</section>
<?php
}
} else { ?>
<h2>Welcome Customer!</h2>
<div >
<p>We are giving away <span >free coupons</span> as token of appreciation for first <?= $_SESSION['counter'] ?> customer(s)</p>
<form action="process.php" method="post">
<label for="name">Kindly type your name: </label>
<input type="text" id="name" name="name_entered">
<input type="submit" id="submit" value="Submit" name="submit">
</form>
</div>
<?php }
?>
</main>
</body>
</html>
This is my process.php:
<?php
session_start();
if(!isset($_SESSION['counter'])){
$_SESSION['counter'] = 10;
} else {
$_SESSION['counter'] = $_SESSION['counter'] - 1;
}
if(isset($_POST['reset_btn'])){
$_SESSION['counter'] = 10;
}
header("Location: index.php");
?>
Thanks in advance!
CodePudding user response:
After redirect with header() function you do not have anything in $_POST anymore. In this case access to posted values you have only in process.php. You can save your posted values in $_SESSION and check those in index.php