Sujeewahms
Sujeewahms

Reputation: 3

increment numaric (integer) variable by clicking a button in php

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

Answers (3)

Kei
Kei

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

tutoground
tutoground

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

Confused
Confused

Reputation: 1662

Use following

<?php
$raw =$raw+intval($_POST['incr']);

?>

Upvotes: 0

Related Questions