Jump to content

multiple choice with radio buttons?


webguync

Recommended Posts

In this form, I am using radio buttons to select various PHP math function results (total, average,both) and it works but was wondering if it is possible to make it multiple choice, that is to say instead of displaying one result at t time when you submit, displaying two or three of the results, depending on how many radio buttons are clicked. Can this be done?

 

Here is the form code

 

<form action="." method="post">
        <input type="hidden" name="action" value="process_scores" />

        <label>Score 1:</label>
        <input type="text" name="scores[]" 
               value="<?php  echo $scores[0]; ?>"/><br />

        <label>Score 2:</label>
        <input type="text" name="scores[]"
               value="<?php  echo $scores[1];  ?>"/><br />

        <label>Score 3:</label>
        <input type="text" name="scores[]"
               value="<?php echo $scores[2];  ?>"/><br />
               
        <!-- ADD LOGIC TO DETERMINE WHETHER TO CALCULATE AVERAGE, TOTAL OR BOTH -->
          <fieldset>
       <legend>  What do you want to calculate?</legend>
        <p>
          <input type="radio" name="calculate" value="average" checked="checked" />
          Average<br />
        <input type="radio" name="calculate" value="total" />Total<br />
        <input type="radio" name="calculate" value="both" />Both</p>
        <p><br />
        </p>
        </fieldset>
        <br /><br />
        <label> </label>
        <input type="submit" value="Process Scores" /><br />

        <label>Scores:</label>
        <span><?php if (isset($scores_string)) { echo $scores_string; } ?></span><br />

        <label>Score Total:</label>
        <span><?php if (isset($score_total)) { echo $score_total; } ?></span><br />

        <label>Average Score:</label>
        <span><?php if (isset($score_average)) { echo $score_average; } ?></span><br />
    </form>

 

and the processing code

<?php

if (isset($_POST['action'])) {
    $action =  $_POST['action'];
} else {
    $action =  'start_app';
}

if (isset($_POST['calculate'])) {
$calculate = $_POST['calculate'];
} 


switch ($action) {
    case 'start_app':
        $scores = array();
        $scores[0] = 70;
        $scores[1] = 80;
        $scores[2] = 90;
        break;
    case 'process_scores':
        $scores = $_POST['scores'];

        // validate the scores
        $is_valid = true;
        for ($i = 0; $i < count($scores); $i++) {
            if (empty($scores[$i]) || !is_numeric($scores[$i])) {
                $scores_string = 'You must enter three valid numbers for scores.';
                $is_valid = false;
                break;
            }
        }
        if (!$is_valid) {
            break;
        }

        // process the scores
        $scores_string = '';
        $score_total = 0;
                
        foreach ($scores as $s) {
            $scores_string .= $s . '|';
            $score_total += $s;
        }
        $scores_string = substr($scores_string, 0, strlen($scores_string)-1);

        // calculate the average
        $score_average = $score_total / count($scores);
        
        // format the total and average
    	switch($calculate) {
    		case 'average':
    			$score_average = number_format($score_average, 2);
    			$score_total = "";
    			break;
    		case 'total':
    			$score_average = "";
    			$score_total = number_format($score_total, 2);
    			break;
    		case 'both':
        		$score_total = number_format($score_total, 2);
        		$score_average = number_format($score_average, 2);
        		break;        	
	}
        break;
    
}
include 'loop_tester.php';
?>

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.