Hello Guys, I have a radio button has a different value i dont know how to get the sum of this. can you give me some ideas? or sample code?

thanks guys. this is my code.

<tr>
                                                <td width="500"><?php echo $behavioral; ?>
                                                </td>
                                                <td  align="center" >
                                                <div>
                                                <input type='radio' class='css-checkbox' id='1radio<?php echo ($id); ?>' name="<?php echo ($id); ?>" value="<?php echo ($count1); ?>"><label for="1radio<?php echo ($id); ?>" class="css-label">1</label>
                                                <input type='radio' class='css-checkbox' id='2radio<?php echo ($id); ?>' name="<?php echo ($id); ?>" value="<?php echo ($count2); ?>"><label for="2radio<?php echo ($id); ?>" class="css-label">2</label>
                                                <input type='radio' class='css-checkbox' id='3radio<?php echo ($id); ?>' name="<?php echo ($id); ?>" value="<?php echo ($count3); ?>" checked='checked'><label for="3radio<?php echo ($id); ?>" class="css-label">3</label>
                                                <input type='radio' class='css-checkbox' id='4radio<?php echo ($id); ?>' name="<?php echo ($id); ?>" value="<?php echo ($count4); ?>"><label for="4radio<?php echo ($id); ?>" class="css-label">4</label>
                                                <input type='radio' class='css-checkbox' id='5radio<?php echo ($id); ?>' name="<?php echo ($id); ?>" value="<?php echo ($count5); ?>" ><label for="5radio<?php echo ($id); ?>" class="css-label">5</label>
                                                </td>
                                                </div>
                                            </tr>

Dani AI

Generated

A short, practical follow-up to : you want a reliable overall total from one radio-per-question. 's PHP approach to compute the sum on submit is the right server-side step. The addition below shows a lightweight client-side complement so the page displays a live total (helpful for users) while still letting your server re-calculate and validate the final value.

Give each radio a common class (for example score) and a visible place for the result (for example a <span id="total">0.00</span>). Listen for changes, collect all checked radios, convert values to numbers, sum using integer arithmetic to avoid floating-point rounding, then format for display. Example (unobtrusive, no libraries):

document.addEventListener('change', function(e) {
  if (!e.target.matches('input[type="radio"].score')) return;
  let cents = 0;
  document.querySelectorAll('input[type="radio"].score:checked').forEach(r => {
    const v = parseFloat(r.value);
    if (!isNaN(v)) cents += Math.round(v * 100); // work in cents
  });
  document.getElementById('total').textContent = (cents / 100).toFixed(2);
});

Server-side must still be authoritative: on submit cast posted values to numbers (float), recompute the same total, and store or use that value. Use simple, consistent name tokens (for example q1, q6) so looping/validation is straightforward.

Troubleshooting tips: watch out for string concatenation (use parseFloat/Number), missing checked inputs (skip NaN), dynamic content (use event delegation as above), and locale decimal separators (users who type commas). Finally, never trust the client — always re-validate on the server before accepting totals.

Recommended Answers

All 7 Replies

<?php
$id = 3; // e.g. value of checked
for($i=1; $i<=5; $i++){
$checked = ($id==$i?" checked=\"checked\"":""); ?>
    <input type="radio" class="css-checkbox" id="1radio<?=($id);?>"<?=$checked;?> name="<?=($id);?>" value="<?=$i;?>">
    <label for="1radio<?=($id);?>" class="css-label"><?=$i;?></label>
<?php } ?>

What is the value of your "$count" sequential or not?

for example. like this
First Option

<input type="radio" class="css-checkbox" id="1radio1" name="1" value="0.8">1
<input type="radio" class="css-checkbox" id="2radio1" name="1" value="1.6">2
<input type="radio" class="css-checkbox" id="3radio1" name="1" value="2.4" checked="checked">3
<input type="radio" class="css-checkbox" id="4radio1" name="1" value="3.2">4
<input type="radio" class="css-checkbox" id="5radio1" name="1" value="4">5

Second Option A

<input type="radio" class="css-checkbox" id="1radio6" name="6" value="2">1
<input type="radio" class="css-checkbox" id="2radio6" name="6" value="4">2
<input type="radio" class="css-checkbox" id="3radio6" name="6" value="6" checked="checked">3
<input type="radio" class="css-checkbox" id="4radio6" name="6" value="8">4
<input type="radio" class="css-checkbox" id="5radio6" name="6" value="10">5

Firts option B

<input type="radio" class="css-checkbox" id="1radio6" name="6" value="2">1
<input type="radio" class="css-checkbox" id="2radio6" name="6" value="4">2
<input type="radio" class="css-checkbox" id="3radio6" name="6" value="6" checked="checked">3
<input type="radio" class="css-checkbox" id="4radio6" name="6" value="8">4
<input type="radio" class="css-checkbox" id="5radio6" name="6" value="10">5

Second Option B

<input type="radio" class="css-checkbox" id="1radio6" name="6" value="2">1
<input type="radio" class="css-checkbox" id="2radio6" name="6" value="4">2
<input type="radio" class="css-checkbox" id="3radio6" name="6" value="6" checked="checked">3
<input type="radio" class="css-checkbox" id="4radio6" name="6" value="8">4
<input type="radio" class="css-checkbox" id="5radio6" name="6" value="10">5

Output. like this
First Option A: 0.8
First Option B:4

Second Option A: 4
Second Option B:8

Overall Total : 16.88

But in My Code. I dont know to to get the SUm.. HElp me please.

EDIT----
In my Radio Name i used by id of each question. like this <?php echo $id; ?>

Use checkbox instead of radio
Something like this:

<?php
$array = array( array(0.8, 1.2, 2.4, 3.2, 4), array(2, 4, 6, 8, 10) );
echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">";
for($a=1; $a<=5; $a++){
    $arr = ($a>1?$array[1]:$array[0]);
    foreach($arr as $key => $val){
        echo "
        <input type=\"checkbox\" id=\"ch_".$a."_".$key."\" name=\"ch_".$a."[]\" value=\"".$val."\" />
        <label for=\"ch_".$a."_".$key."\">".$val."</label>";
        }
    echo "<br/>";
    }
echo "<input type=\"submit\" name=\"submit\" value=\"submit\" /></form>";

if(isset($_POST['submit'])){
$total = 0;
for($a=1; $a<=5; $a++){
    if(isset($_POST['ch_'.$a])){
        $cur = array_sum($_POST['ch_'.$a]);
        echo $a."-sum = ".$cur."<br/>";
        $total += $cur;
        }
    }
echo "Total: ".$total;
}
?>

Sir, I think I cant Use Checkbox, Can You Check it Out hEre. This is My Form Layout.

Thats why i used Radio instead Checkbox

<?php
$array = array( array(0.8, 1.2, 2.4, 3.2, 4), array(2, 4, 6, 8, 10) );
echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">";
for($a=1; $a<=5; $a++){
    $arr = ($a>1?$array[1]:$array[0]);
    foreach($arr as $key => $val){
    $checked = ((isset($_POST['radiobutton_'.$a])&&$_POST['radiobutton_'.$a]==$val)?" checked=\"checked\"":"");
    echo "
    <input type=\"radio\" id=\"ch_".$a."_".$key."\"".$checked." name=\"radiobutton_".$a."\" value=\"".$val."\" />
        <label for=\"ch_".$a."_".$key."\">".($key+1)."</label>";
        }
    echo "<br/>";
    }
echo "<input type=\"submit\" name=\"submit\" value=\"submit\" /></form>";

if(isset($_POST['submit'])){
    $total = 0;
    for($a=1; $a<=5; $a++){
        if(isset($_POST['radiobutton_'.$a])){
            $cur = $_POST['radiobutton_'.$a];
            echo $a."-sum = ".$cur."<br/>";
            $total += $cur;
            }
        }
    echo "Total: ".$total;
    }
?>

Let me Try Your Code Sir, Ill Update you later for what will happen.. thanks sir.

And by the way ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
http://www.w3.org/TR/html4/types.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.