Jump to content

Adding Radial Button Selections Together


nepzap2

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.