i have a file user_submit.php that handle all the POST requests.
what i am doing is i am sending small integer values through jquery POST method. these integer values are sent one at a time around 10 times.
what i want to do is i want to add all these values up and provide a final result after the last step.
so i am doing:
$score = intval($_POST['score']);
$total = $total+$score;
echo $total;
but it fails to sum all the values up. as soon as i send the second value, the file forgets the first value. i mean the first value doesnt get stored in $total for use in the second request.
how do i go about it?
The problem exists because you have a wrong understanding of the whole way PHP (and other web-based languages work).
Once each PHP request is done, the application is closed, all variables are killed, etc. Therefore every time your POST request finishes, your
$totalvariable is reset.To work around this limit, PHP has a session handling mechanism that allows you to store variables session-wide, thus keeping them between the requests, but allowing them to be set uniquely for each user.
I suggest that you modify so that it uses the build-in PHP session handling mechanism:
You can find more examples of session variable usage in the PHP manual.