Forum Moderators: coopster

Message Too Old, No Replies

Adding Checkbox Values? Is it Possible?

Trying to implement an experience point system.

         

methylparabex

4:10 pm on Apr 21, 2007 (gmt 0)

10+ Year Member



Hello everyone, let me be the first to say that this site has been nothing but incredible for an up and coming coder and really appreciate all the work you do! Thank you!

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

jatar_k

6:50 pm on Apr 21, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld methylparabex,

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

cameraman

6:52 pm on Apr 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, methylparabex!

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!

methylparabex

8:57 pm on Apr 21, 2007 (gmt 0)

10+ Year Member



Yes, I have a MySQL database that will be storing all of the information and plan on inserting these values into a table for storage. I'm unfamilar with how exactly foreach would come into play in this situation, just for an example with the syntax you've used eariler, could you give me an idea of what the foreach code would look like to extract the values out of the array? I'm sorry if this sounds naive, but I guess I'm still learning, haha.

methylparabex

9:13 pm on Apr 21, 2007 (gmt 0)

10+ Year Member



As I'm experimenting, would the result look something like this?

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

cameraman

10:04 pm on Apr 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're very close:
foreach($_POST['exercise'] as $points) {

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).