Jump to content

Adding Up Values From Radio Buttons


Frieling

Recommended Posts

Alright so i am a decent PHP coder. but i dont use radio buttons that much, Ive tried to google for that. But i need to use radio buttons and assign it each a value. 1, 2 , and 3. then after the user selects which ever radio button they want, it will add up how many time either 1, 2, or 3 was selected, and its put into a MySQL database. I have no clue how to do this, so any ideas?

Link to comment
Share on other sites

I just need to learn how to add up the values overall. The PHP/HTML tutorials do not help this case.... Heres some pseudo code: Radio button1, value = 1Radio button2, value = 2Radio button3, value = 3 Person chooses RB1 3 times, RB3 2 times, and RB2 2 times. Add the value of RB1 3 times, add the value of RB3 2 times, and add the value of RB2 2 times. Insert into tables of MySQL DB. Thats what i need to do. Idk how to do it. Does that help?

Link to comment
Share on other sites

Give each radio button the same name attribute and just check the value of $_POST['name'] or $_GET['name'] (depending on your form method) Wen you don't know what to expect from a form input, just do tests and print out the $_POST array to see what it looks like:

// See what was sent:print_r($_POST);exit;

Link to comment
Share on other sites

You need send the value the button to the submit page and have php add it up there. If you want the user to keep clicking it all on one page then you can use JavaScript to record and count them until there ready to be submitted. If you need it to update the database without laving the page then AJAX is your answer. Note: Sorry, me and Ingolme posted at the same time...

Link to comment
Share on other sites

A database to store monodimensional information like this might be a lot. You'll either have one record with three fields or three records with one field (two fields, but only one with actual data, the other is used as an identifier) When you receive the data, use the UPDATE query for whichever option was selected.An example would be something like this: (Assuming a table with three records and two fields (id and value)UPDATE table SET value = value + 1 WHERE id = '$pressed_button'

Link to comment
Share on other sites

Regarding your pseudocode, how does one select a radio button multiple times?

Link to comment
Share on other sites

maybe your after something like

<input type='radio' name='test' value='1' onclick='increaseRadiosClicked(1)' /> A<input type='radio' name='test' value='2' onclick='increaseRadiosClicked(2)' /> B<input type='radio' name='test' value='3' onclick='increaseRadiosClicked(3)' /> C

radiosClicked = [];function increaseRadiosClicked(value) {if (radiosClicked[value])radiosClicked[value]++;elseradiosClicked[value] = 1;} function uploadRadiosClicked() {var query = [];for (var i=1; i<4; i++) {query.push('rb['+i+']='+(radiosClicked[i] ? radiosClicked[i] : 0));}query = query.join('&'); // send ajax with query on end of URL}

then in PHP you can access the amounts with $_POST['rb'][1], $_POST['rb'][2] and $_POST['rb'][3], or $_GET depending on the ajax method you used.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...