Reputation: 188
I have a form with a button "NEXT", which on clicked needs to change the value of a variable.I have initialized the variable as 0 and I want that as the NEXT button is getting clicked the variable should increment by 1.Since, I am also submitting the form when the button is clicked,so the variable is resetting to 0,it is not keeping the previous value.
html code is :
<form name="graph" enctype="multipart/form-data" method="POST" action="graph.php">
<input type="hidden" id="hdn" name ="hdn" value="">
<input type="button" id="btnNext" name ="btnNext" value="NEXT" onClick="nextfun();">
Onclicking NEXT, I am calling a javascript function that is submitting the form,
function nextfun()
{
var a= document.getElementById("hdn");
a.value="nextgroup";
document.graph.submit();
}
Now,when form is submitted, I am using php code to increment the value of variable.
$flag=0;
if(isset($_POST['hdn']))
{
if($_POST["hdn"]=="nextgroup")
{
$flag=$flag+1;
}
}
Upvotes: 1
Views: 5819
Reputation: 72299
On the graph.php
page do like this:-
<?php
session_start();
if(isset($_POST['hdn']))
{
if($_POST["hdn"]=="nextgroup")
{
if(isset($_SESSION['flag'])){
$_SESSION['flag'] = $_SESSION['flag']+1;
}else{
$_SESSION['flag'] = 1;
}
}
}
echo $_SESSION['flag'];
?>
Note:- first time Session value will 1and nxt onward on each submission it will increase by 1.
Upvotes: 1
Reputation: 1219
You can add to your HTML
something like this:
<input type="hidden" name="prevValue" value="<?php echo $prev; ?>" />
You should submit this value too.
And in the PHP
$flag=0;
if(isset($_POST['hdn']))
{
if($_POST["hdn"]=="nextgroup")
{
$prev = $_POST["prevValue"];
$flag = $prev +1;
}
}
Upvotes: 0