nepzap2 Posted June 1, 2009 Share Posted June 1, 2009 Hello All, I want to be able to select any number of the radial buttons below. The I want to be able to add the value of these options and store the added value in the mySQL database upon clicking the submit button. Do any of you know how to do this? Any help is extremely appreciated. <table ID="poll" width="125" cellpadding="2" cellspacing="0" border="0"> <tr align="center" valign="top"> <td align="left" colspan="1" rowspan="1"> <h4>Select your favorite color.</h4> <form method="POST" action="simple-poll.php"> <input type="radio" name="check[]" value="1">One<br> <input type="radio" name="check[]" value="2">Two<br> <input type="radio" name="check[]" value="3">Three<br> <input type="radio" name="check[]" value="4">Four<br> <input type="submit" value="Add"> </form> </td></tr></table> Link to comment https://forums.phpfreaks.com/topic/160504-adding-radial-button-selections-together/ Share on other sites More sharing options...
anupamsaha Posted June 1, 2009 Share Posted June 1, 2009 The concept of radio button is to have only one selection from a group of selection. Form a UI perspective, can you use "checkbox" instead of radio button? Also, by using checkbox, your problem will be solved too. <table ID="poll" width="125" cellpadding="2" cellspacing="0" border="0"> <tr align="center" valign="top"> <td align="left" colspan="1" rowspan="1"> <h4>Select your favorite color.</h4> <form method="POST" action="simple-poll.php"> <input type="checkbox" name="check[]" value="1">One<br> <input type="checkbox" name="check[]" value="2">Two<br> <input type="checkbox" name="check[]" value="3">Three<br> <input type="checkbox" name="check[]" value="4">Four<br> <input type="submit" value="Add"> </form> </td></tr></table> Link to comment https://forums.phpfreaks.com/topic/160504-adding-radial-button-selections-together/#findComment-847037 Share on other sites More sharing options...
nepzap2 Posted June 1, 2009 Author Share Posted June 1, 2009 Thank You for replying anupamsaha . So what you are saying is that if I use check boxes my problem is solved? Let's say I check one and three and my query inserts these value in my database. Is it going to be stored as 4 and then when I retrieve it it will read four? Is this o.k., I'm going to test it. Link to comment https://forums.phpfreaks.com/topic/160504-adding-radial-button-selections-together/#findComment-847043 Share on other sites More sharing options...
anupamsaha Posted June 1, 2009 Share Posted June 1, 2009 Sorry, my mistake. Will there be multiple selection, or only one choice? Link to comment https://forums.phpfreaks.com/topic/160504-adding-radial-button-selections-together/#findComment-847049 Share on other sites More sharing options...
nepzap2 Posted June 1, 2009 Author Share Posted June 1, 2009 Multiple selections Link to comment https://forums.phpfreaks.com/topic/160504-adding-radial-button-selections-together/#findComment-847057 Share on other sites More sharing options...
anupamsaha Posted June 1, 2009 Share Posted June 1, 2009 Multiple selections Checkbox is the correct option. But, I am not clear about getting 4 from db, while you choose 1 and 3. Can you please explain it more? Link to comment https://forums.phpfreaks.com/topic/160504-adding-radial-button-selections-together/#findComment-847064 Share on other sites More sharing options...
nepzap2 Posted June 1, 2009 Author Share Posted June 1, 2009 when you choose 1 and 3, it should store as 4. right. basically adding the number that corresponds to the check boxes. Thanks, Link to comment https://forums.phpfreaks.com/topic/160504-adding-radial-button-selections-together/#findComment-847066 Share on other sites More sharing options...
anupamsaha Posted June 1, 2009 Share Posted June 1, 2009 when you choose 1 and 3, it should store as 4. right. basically adding the number that corresponds to the check boxes. Thanks, If you get the logic correct, then surely it will store 4. And, you will have 4 when retrieving from db. Link to comment https://forums.phpfreaks.com/topic/160504-adding-radial-button-selections-together/#findComment-847069 Share on other sites More sharing options...
nepzap2 Posted June 1, 2009 Author Share Posted June 1, 2009 Thank you for your help. This is the form <form method="POST" action="addTotal.php"> <input type="checkbox" name="check[]" value="1">One<br> <input type="checkbox" name="check[]" value="2">Two<br> <input type="checkbox" name="check[]" value="3">Three<br> <input type="checkbox" name="check[]" value="4">Four<br> <input type="submit" value="Add"> </form> this is the processing code. <?php include_once("db_config.php"); $total_number = $_POST["check"]; $sql = "INSERT INTO add ('', total_number) VALUES('', '$total_number')"; $result = mysql_query($sql) or die(mysql_error()); header( 'Location: ../SummerInternStatsTEST.php' ) ; ?> But I'm getting this error. "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'add ('', total_number) VALUES('', 'Array')' at line 1" Do you know what I'm doing wrong Link to comment https://forums.phpfreaks.com/topic/160504-adding-radial-button-selections-together/#findComment-847073 Share on other sites More sharing options...
anupamsaha Posted June 1, 2009 Share Posted June 1, 2009 Please try this: <?php include_once("db_config.php"); $array_of_numbers = $_POST["check"]; $total_number = 0; foreach($array_of_numbers as $number) { $total_number += $number; } $sql = "INSERT INTO add ('', total_number) VALUES('', '$total_number')"; $result = mysql_query($sql) or die(mysql_error()); header( 'Location: ../SummerInternStatsTEST.php' ) ; ?> While you post the form, the check boxes will be available as an array to the PHP script. You have to iterate through the array and do the sum of the chosen numbers. Thats what I am doing above. Hope this will help. Link to comment https://forums.phpfreaks.com/topic/160504-adding-radial-button-selections-together/#findComment-847113 Share on other sites More sharing options...
nepzap2 Posted June 1, 2009 Author Share Posted June 1, 2009 Thank You anupamsaha but it gives me this error "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'add (total_number) VALUES('10')' at line 1" DiD "$total_number" change because of this line $total_number += $number;? Link to comment https://forums.phpfreaks.com/topic/160504-adding-radial-button-selections-together/#findComment-847161 Share on other sites More sharing options...
anupamsaha Posted June 1, 2009 Share Posted June 1, 2009 Thank You anupamsaha but it gives me this error "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'add (total_number) VALUES('10')' at line 1" DiD "$total_number" change because of this line $total_number += $number;? The change is due to: $array_of_numbers = $_POST["check"]; $total_number = 0; foreach($array_of_numbers as $number) { $total_number += $number; } Regarding the SQL error, can you please provide the structure of "add" table? Why the insert statement like this? $sql = "INSERT INTO add ('', total_number) VALUES('', '$total_number')"; Link to comment https://forums.phpfreaks.com/topic/160504-adding-radial-button-selections-together/#findComment-847173 Share on other sites More sharing options...
nepzap2 Posted June 1, 2009 Author Share Posted June 1, 2009 Thank you so much for helping me. I basically want to leave the auto increment table alone. the structure of the table "add" is simple. id int(11) auto increment primary key total_number int(11) Link to comment https://forums.phpfreaks.com/topic/160504-adding-radial-button-selections-together/#findComment-847180 Share on other sites More sharing options...
anupamsaha Posted June 1, 2009 Share Posted June 1, 2009 Thank you so much for helping me. I basically want to leave the auto increment table alone. the structure of the table "add" is simple. id int(11) auto increment primary key total_number int(11) Ok. Change the INSERT sql as: $sql = "INSERT INTO `add` (`total_number`) VALUES('$total_number')"; No need to use '' for an auto incremented field. For an INSERT, it will be automatically incremented. And always use back quote characters for table and field name. It will reduce conflict between MySQL reserved words. Hope this will help you. Link to comment https://forums.phpfreaks.com/topic/160504-adding-radial-button-selections-together/#findComment-847184 Share on other sites More sharing options...
nepzap2 Posted June 1, 2009 Author Share Posted June 1, 2009 Thank You So Much anupamsaha it works now. Many Thanks Link to comment https://forums.phpfreaks.com/topic/160504-adding-radial-button-selections-together/#findComment-847189 Share on other sites More sharing options...
anupamsaha Posted June 1, 2009 Share Posted June 1, 2009 Thank You So Much anupamsaha it works now. Many Thanks Welcome. Please mark the topic as "solved". Link to comment https://forums.phpfreaks.com/topic/160504-adding-radial-button-selections-together/#findComment-847190 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.