Otag Otag - 7 months ago 9
PHP Question

How to add (or subtract) a value by pressing the submit button PHP

I have the following code=>

<button class="btn btn-lg btn-warning text-center but-width" id="first" name="geri" type="submit" value="<?php ?>">BACK</button>
<button class="btn btn-lg btn-warning text-center but-width" id="second" name="ileri" type="submit" value="<?php ?>">FORWARD</button>


These buttons submit the form to the same page. At the top of the page I have variable $t which is initialized at value '0' and is followed by the codes=>

$t=0;


if( $this->input->post('ileri') ) { $t=$t+1;} // Back button was pressed;
if( $this->input->post('geri') ) { $t=$t-1;} // Forward button was pressed;


Now as I press forward I want to increase $t and when I press back I want to decrease it. But because of the declaration at the top ($t=0;) it goes as far as '1' with 'forward' and as back as '0' with 'back' buttons. Can anyone think of a way of getting over this problem.
Thank you...

Answer

Add a hidden input, and store $t there. This gets posted upon submit:

<?php
$t = $this->input->post('t');
if (!$t) $t = 0;
if( $this->input->post('ileri') ) { $t=$t+1;} // Back  button was pressed; 
if( $this->input->post('geri') ) { $t=$t-1;} // Forward button was pressed;
?>
<input name="t" type="hidden" value="<?=$t ?>"> 
<button class="btn btn-lg btn-warning text-center but-width" id="first" name="geri" type="submit" value="submit">BACK</button>   
<button class="btn btn-lg btn-warning text-center but-width" id="second" name="ileri" type="submit" value="submit">FORWARD</button>
Comments