2

I want to sum values in a php variable from html text box my php code is as under

$total = 0;
$val2 = $_POST['val1'];
$total += val2;
echo "Sum of value is ". $total;

This is my HTML:

<form action="add.php" method="post" >
  <input type="text" name="val1" />
  ...
  ...
6
  • this is my html code Commented Nov 26, 2015 at 11:35
  • '<form action="add.php" method="post" >' '<input type="text" name="val1" />' Commented Nov 26, 2015 at 11:36
  • Sum should then be $total Commented Nov 26, 2015 at 11:37
  • when i give first value in text box it shows the first value ,but as i give the value second time it doesnot sum the previous value with new one and display only last value given Commented Nov 26, 2015 at 11:37
  • Because PHP is a server side programming language and it runs at first. next time u will need to refresh the page Commented Nov 26, 2015 at 11:43

5 Answers 5

2

use Session

session_start();
$total = 0;
$val2 = $_POST['val1'];
$_SESSION["total"]+= $val2;
echo "Sum of value is ". $_SESSION["total"];

A session is a way to store information (in variables) to be used across multiple pages

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

3 Comments

What will happen if the $_SESSION["total "] is not set ..? especially when it runs for the first time? @Rahautos
i tried it but it doesn't working ,it is showing "sum of value is val2 " every time i enter a value in text box
session_start(); $total = 0; $val2 = $_POST['val1']; $_SESSION["total"]+= $val2; echo "Sum of value is ". $_SESSION["total"];
1

Use sessions;

session_start();
$val2 = $_POST['val1'];
$total = 0;
if(isset($_SESSION['prev_sum'])){
    $total = $_SESSION['prev_sum'] + $val2;
}else{
    $total += $val2;
}
$_SESSION['prev_sum'] = $total;

echo "Sum of value is ". $total;

2 Comments

i tried this code but it is not sum the second value given in text box .
it set the $total value to 0 when i enter value in text box next time
0

$total+=$val2 refers to $total = $total + $val2 so the values are being summed up in the variable $total instead of $val2. So replace $val2 in echo statement with $total.

Comments

0

you have to use sessions or database(which is not suitable for your current problem) for this because http is stateless protocol i-e it cannot maintain its state so as soon as new request originates all the variables set to its default values

session_start();
$sum= 0;
$val2 = $_POST['val1'];
$_SESSION["sum"]+= val2;
echo "Sum of value is ". $_SESSION["sum"];

Comments

0

It should be something like

session_start();
$val1 = isset($_SESSION['val2']) ? $_SESSION['val2'] : 0 ;
$val2 = $_POST['val2'];
$total = $val1 + $val2;
$_SESSION['val2'] = $total;
echo "Sum of value is ". $total;

AND

<form action="add.php" method="post" >
    <input type="text" name="val1" value="<?php echo $_POST['val1'] ?>" />
    <input type="text" name="val2" />
....

3 Comments

it showing the value i entered in text box .but when i enter next value in text box it does't sum the second value in $total variable
no sir i want to use one text box ,every time i enter a value in text box it should be sum in $total variable and shows the result new value
In that case, You can use the solution provided by `William Madede. use session to store old value. I have editied the answer. please have a look

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.