can a POST file handle multiple requests?
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 $total
variable 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:
<?php
$score = intval($_POST['score']);
$_SESSION['total'] = isset($_SESSION['total']) ? $_SESSION['total']+$score : $score;
echo $_SESSION['total];
You can find more examples of session variable usage in the PHP manual.
You will need to store $total
in a $_SESSION
variable on the php side otherwise it gets recreated at each request.
Alternatively you could pass both score and current total via ajax.
精彩评论