Reputation: 3
I need to add and subtract to an integer variable in PHP by one, when I click the button using PHP and HTML.
i'm should not insert any number in this scene.
<HTML> <input type = "submit" name = "incr" value = '1'/> </HTML>
can i use this code.
<?php $raw = $_POST['incr']+$raw; ?>
every time when i click the button, the $raw should increase by one and if i click decrease button it should subtract by one.
Thank you.
Upvotes: 0
Views: 2308
Reputation: 771
Use SESSIONS
<?php
session_start();
$_SESSION['raw'] += intval($_POST['incr']);
?>
Also if you want to decrement $_SESSION['raw'] just add a simple if-else with POST variable in it.
Upvotes: 1
Reputation: 96
<?php
print_r($_GET);
if(isset($_GET['current_value']))
{
if(isset($_GET['valinc']))
{
$current_value=$_GET['current_value']+1;
}else{
$current_value=$_GET['current_value']-1;
}
}else{
$current_value=0;
}
?>
<form name="form1" method="get">
<input type="text" name="current_value" value="<?php echo $current_value; ?>"/>
<input type="submit" name="valinc" value="+1" />
<input type="submit" name="valdec" value="-1" />
</form>
check this simple php form script.
Upvotes: 0