Forum Moderators: coopster
And I'm starting a fitness website where individuals can submit their daily progress through checkboxes indicating certain exercises or daily accomplishments. For example, a cardio workout giving 2 experience points or an upper body workout giving 4 experience points. Is it possible to cumulatively add these values upon submission of a form? To keep track of daily progress? If anyone needs further explanation or clarification please let me know, I'd really love to get a solution. Thanks again for your time.
Nick
do you plan on using a database to save the info the user submitted?
you could have a seperate table that just stored the totals, or even put it in the user table, assuming there is one
this is a wild guess since I don't know anything about how you are setting it up
I think the easiest way to do it would be to use the points as the checkbox values and give them all the same name followed by square brackets: []
<input type="checkbox" name="exercise[]" value="2">Cardio
In your form processing script, those come to you as an array: $_POST['exercise']
Use the foreach [us.php.net] construct to walk through the array and total up the values.
It's good pratice to 'expect the unexpected' with any data that's given you by a user, that is, don't depend on the values coming back being the ones you sent out. In this case, you can use intval() [us.php.net] to check the validity of the point data. Also prepare for the possibility that someone submits the form without checking any of the boxes. In that event, $_POST['exercise'] won't exist at all - you can use isset() [us2.php.net] to make that determination. If only one box is checked, $_POST['exercise'] will exist but won't be an array - use the is_array() [us.php.net] function to make that determination.
Take a stab and let us know how you fare!
<?php
foreach ($exercise as $points) {
$totalxp = $points + $totalxp
}
echo "Your total XP for today is '$totalxp'."
?>
That should take care of the total correct? In the processing script, how would I insert $totalxp into a table if it is not actually being processed via the submission form?
It's entirely possible that what you posted works. If that's the case, that means something called register_globals [us2.php.net] is set to on with your php configuration. Much of the php community views this as a security hazard. The issue may still be arguable, but at the very least, with shared hosting it's always a distinct possibility that your host will up and decide to turn it off some Monday morning and your scripts stop working.
And as extra security, it's better to do this on data which should oughta be whole numbers:
$totalxp = intval($points) + $totalxp; // Don't forget this semicolon
You'll use mysql_query() [php.net] to do the table insertion. Here's a library thread which will lead you to the sql - sql commands [webmasterworld.com] (jatar_k's post most of the way down the page).