0

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.

3
  • are you saving the value in db? or just want to display it? Commented Jul 29, 2014 at 11:11
  • Use jQuery + ajax if you want to save it to db Commented Jul 29, 2014 at 11:13
  • variable should be use in php. it will not save to DB. Commented Jul 29, 2014 at 11:15

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Use following

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

?>

Comments

0
<?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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.